Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

JSON Array inside an array

Im trying to put some data in, for json_decode, but I want an array inside an array. The inner array I dont have any names for, its just a list, so I thought the following would work:-
{
"type" : "combo",
"attribute" : "LIST",
"Values" : {"Male","Female"} 
}

Open in new window


However it doesnt seem to decode anything when I try it with:-
$strJSONTest = '{"type" : "combo", "attribute" : "LIST", "Values" : {"Male","Female"} }';
$jsonArr = json_decode($strJSONTest);
print_r $jsonArr['type'];

Open in new window


I know its the inner array, as the following code works:-
$strJSONTest = '{"type" : "combo", "attribute" : "LIST", "Values" : "Test" }';
$jsonArr = json_decode($strJSONTest);
print_r $jsonArr['type'];

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

http://www.json.org/
Why not create the data structure you want, and then use JSON_Encode() to create the JSON string?  Easy!

Keep in mind, however, that JSON will decode a JSON String into an object with UTF-8 encoding.  That is just how it works.  So even if you start with an outer array you will get an object back from the process.  Inner arrays are preserved.
http://www.laprbass.com/RAY_temp_tonelm54.php

<?php // RAY_temp_tonelm54.php
error_reporting(E_ALL);

$jso = <<<EOD
{
"type" : "combo",
"attribute" : "LIST",
"Values" : {"Male","Female"}
}
EOD;

// CREATE THE TEST DATA
$dat = array
( 'type'      => 'combo'
, 'attribute' => 'LIST'
, 'Values'    => array( 'Male', 'Female' )
)
;

// ENCODE INTO A JSON STRING FOR TRANSPORT
$jso = json_encode($dat);

// DECODE THE STRING INTO AN OBJECT
$obj = json_decode($jso);

// WHAT IS IN THE ORIGINAL AND THE OBJECT?
var_dump($dat);
var_dump($obj);

Open in new window

$strJSONTest = '{"type" : "combo", "attribute" : "LIST", "Values" : {"1": "Male", "2": "Female"} }';

This will fix it.
The matter is it can't analyze the json cause it's invalid.
@ienaxxx: It will create a valid JSON string, but it will not decode the inner structure Values into an array.  To get the array, you would need to use brackets instead of curly braces.

<?php // RAY_temp_ienaxxx.php
error_reporting(E_ALL);

$jso = <<<EOD
{
"type" : "combo",
"attribute" : "LIST",
"Values" : ["Male","Female"]
}
EOD;

// DECODE THE STRING INTO AN OBJECT
$obj = json_decode($jso);

// WHAT IS IN THE OBJECT?
var_dump($obj);

Open in new window

I have this result:
stdClass Object ( [type] => combo [attribute] => LIST [Values] => stdClass Object ( [1] => Male [2] => Female ) )

Open in new window


and this if i use the boolean ASSOC as the second parameter. ( $jsonArr = json_decode($strJSONTest, true);   )

Array ( [type] => combo [attribute] => LIST [Values] => Array ( [1] => Male [2] => Female ) )

Open in new window



So i think you are wrong. :-)
Avatar of gr8gonzo
ienaxxx beat me to the punch. I always use the optional "true" parameter with json_decode so I get the original array back instead of an object.
Wrong, or at best, Lazy!  I find that the object-oriented notation is so much easier to write that I actively avoid using an array if I can use an object instead.  For every property I save a few keystrokes and eliminate the fiddly punctuation in the brackets and quotes.  I never even looked at the second parameter of JSON_Decode()!  Also, I have not tested the notation for transport so I don't know what it would do in JavaScript or another programming language.
The JSON results should be the same, no matter what method you use to encode/decode. If it's a plain array where keys aren't explicitly set, then encoding should produce the square-bracket notation that Ray mentioned: [ "X", "Y", "Z" ].

What ienaxxx produced was technically wrong, because it would change the keys (since array keys start at 0), but his formatting was technically correct:

...{"1": "Male", "2": "Female"} ...
Will produce:
[1] => Male
[2] => Female

[ "Male", "Female"]
Will produce:
[0] => Male
[1] => Female

...{"0": "Male", "1": "Female"} ...
Will produce:
[0] => Male
[1] => Female

So if you don't need to specify the keys/indexes, then use the square bracket notation for arrays. If you customize your keys, then use the { "Key" : "Value" } notation. The notation itself isn't "object-oriented" or "array-oriented". The only thing that turns a JSON string into an object or an array is the decoding step.
Just to be extra-clear, the reason that decoding didn't work is because you tried to use { } brackets without specifying the keys.

{ } means that you are going to specify the contents in the format of:
"KeyA" : "ValueA", "KeyB" : "ValueB"

[ ] means that you are going to specify the contents in the format of:
"ValueA", "ValueB"
(and JSON decoding provides the keys)

So:
{"Male","Female"} is wrong

["Male","Female"] is right
{"0":"Male","1":"Female" } is also right
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Strange choice for the answer. I would have expected 39180990.