0

I have a problem with JSON data in PHP. I need to use data from this JSON in my SQL statement. When I'm trying to debug it with echo(var_dump, or print_r is not working too) command the output with is

{"records":"tekst","name":"[object Object]"}

This is a JSON structre:

            {
                records: 'tekst',
                name: {
                    imie: 'imie1',
                    nazwisko: 'nazwisko1'
                }
            }

I'm trying to decode this by json_decode(), but I have an error

"Warning: json_decode() expects parameter 1 to be string, array given".

Does anyone know what's wrong?

2
  • When I use valid JSON, it works Commented May 6, 2014 at 10:30
  • The error @user3250669 is getting suggests that the already parsed JSON-data structure (PHP array) is being passed to json_decode, rather than the (unparsed) JSON string. edit Commented May 6, 2014 at 10:31

1 Answer 1

1

PHP manual about JSON and the format required: function.json-decode. basically, double quotes only and names must be quoted.

a demonstration of conversion using PHP.

So, you supply the json string that looks like, with the whitespace removed, like this:

{records:[{id:1,name:'n1'},{id:2,name:'n2'}]}

Which is an object containing an array with two entries that could be arrays or objects.

Except, it is not a valid JSON string as it contains single quotes. And PHP wants all the names in double quotes, as in "id":1.

So, possible PHP code to recreate that, assuming arrays as the inner entries is:

$json = new stdClass();
$records = array();
     $entry = array('id' => 1, 'name' => 'n1');
     $records[] = $entry;

     $entry = array('id' => 2, 'name' => 'n2');
     $records[] = $entry;
$json->records = $records;

$jsonEncoded = json_encode($json);

Which, when 'dump'ed looks like:

object(stdClass)[1]
  public 'records' => 
    array
      0 => 
        array
          'id' => int 1
          'name' => string 'n1' (length=2)
      1 => 
        array
          'id' => int 2
          'name' => string 'n2' (length=2)

Now, the string that structure produces is:

{"records":[{"id":1,"name":"n1"},{"id":2,"name":"n2"}]}

Which looks similar to yours but is not quite the same. Note the names in double quotes.

However, if your json string looked the same then PHP could decode it, as is shown below:

$jsonDecoded = json_decode($jsonEncoded);

var_dump($jsonDecoded, 'decoded');

Output: Note all objects...

object(stdClass)[2]
  public 'records' => 
    array
      0 => 
        object(stdClass)[3]
          public 'id' => int 1
          public 'name' => string 'n1' (length=2)
      1 => 
        object(stdClass)[4]
          public 'id' => int 2
          public 'name' => string 'n2' (length=2)

We may want arrays instead so use the true as the second parameter in the 'decode'

$jsonDecoded = json_decode($jsonEncoded, true);

var_dump($jsonDecoded, 'decoded with true switch');

Output: with arrays rather than objects.

array
  'records' => 
    array
      0 => 
        array
          'id' => int 1
          'name' => string 'n1' (length=2)
      1 => 
        array
          'id' => int 2
          'name' => string 'n2' (length=2)
string 'decoded with true switch' (length=24)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.