Decode JSON with square brackets Decode JSON with square brackets json json

Decode JSON with square brackets


try this:

var_export( json_decode( '[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"}]' )  );

json_decode return array or object . you can print it with var_export not echo

and you can access to values :

$items = json_decode('[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"}]');foreach( $items as $each ){  echo $each->location[0]->building[0];  echo '<hr />';  echo $each->location[0]->name;  echo '<hr />';  echo $each->name; // default organization}


Your json is valid , might be you are facing problem while accessing the objects inside the array.

print_r is always a good friend to understand array structure . try this

    $json = '[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"}]';$decoded = json_decode($json);echo '<pre>';print_r($decoded);$location = $decoded[0]->location;$building = $location[0]->building[0];$name = $location[0]->name;

Object at place 0 will only return the first item , if your array has multiple values then use foreach


Seems its a valid JSON.

$my_json = '[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"}]';$my_data = json_decode($my_json);print_r($my_data);

// Output

Array(    [0] => stdClass Object        (            [location] => Array                (                    [0] => stdClass Object                        (                            [building] => Array                                (                                    [0] => Default Building                                )                            [name] => Default Location                        )                )            [name] => Default Organization        ))