0

I have the following JSON String

{
        "name":"Product",
        "properties":
        {
                "id":
                {
                        "type":"number",
                        "description":"Product identifier",
                        "required":true
                },
                "name":
                {
                        "type":"string",
                        "description":"Name of the product",
                        "required":true
                },
                "price":
                {
                        "type":"number",
                        "minimum":0,
                        "required":true
                },
                "tags":
                {
                        "type":"array",
                        "items":
                        {
                                "type":"string"
                        }
                },
                "stock":
                {
                        "type":"object",
                        "properties":
                        {
                                "warehouse":
                                {
                                        "type":"number"
                                },
                                "retail":
                                {
                                        "type":"number"
                                }
                        }
                }
        }
}    

I would like to access

properties - > stock - > properties - > warehouse.

In python I can do the following.

f = open("c:/dir/jsondec.json")
data = json.load(f)
node = data['properties']['stock']['properties']['warehouse']
print str(node)

I'm trying to do the same thing in PHP. I know I can use json_decode() but what should be the correct syntax. Also If I have an array within say within properties-> ID I could have done ['properties'][0]['id'] to access that. What would be the equivalent in php?

1

3 Answers 3

4

Version in Python

This is in Python:

f = open("c:/dir/jsondec.json")
data = json.load(f)
node = data['properties']['stock']['properties']['warehouse']
print str(node)

Version in PHP

And this is its equivalent in PHP:

$f = file_get_contents('c:/dir/jsondec.json');
$data = json_decode($f, true);
$node = $data['properties']['stock']['properties']['warehouse'];
echo $node;

"Gotchas" (or "slight differences")

There is one difference however: f in Python version is opened file, while $f in PHP version is already a string.

As RPM correctly noted, there is another difference: arrays in PHP are converted to string "Array" when used in string context (see here: http://ideone.com/XJfSP), so you probably would like to use:

print_r($node);

or

var_dump($node);

instead of

echo $node;

to see the actual content of the array.

EDIT: Changed json_decode() result into array.

Sign up to request clarification or add additional context in comments.

6 Comments

$node is an associative array. Therefore, echoing node results in an array, you have to either echo $node['type'] or print_r($node)
@RPM: Exactly, but it is the default way to convert array into string. Every array in string context will be displayed as Array as far as I remember.
Yeah for sure. I thought he would want to access the key / value from the warehosue array.
I am pretty sure that you need to update the json_decode($f) as json_decode($f, true), or use $data->properties-> bla bla ... instead
Yep, the output of json_decode without arguments is an object, not an assoc array
|
0

try using json_decode and casting the result as an object.

$r = file_get_contents('json.json');

$jsonObj = (json_decode($r));

$warehouseObj = $jsonObj->properties->stock->properties->warehouse->type;

d($warehouseObj);//to view warehouse node
d($jsonObj);// toview complete object

function d($l){
print "<pre>";
print_r($l);
print "</pre>";
}

2 Comments

The result is already an object, IIRC. Assuming the JSON represents an object of course...and that you haven't said json_decode($r, true).
actually there is no need to send $assoc= true because , you can parse the object retrieved by json_decode directly.
0

In PHP, you can use json_decode to turn an object into an associate array. Just make sure you pass TRUE as the second argument in the json_decode function.

   <?php
       $data = @file_get_contents('c:/xampp/htdocs/json.json');

      if(!$data)
      {
       echo "My bad";
      }
      $newData = json_decode($data,true);

      $a = $newData['properties']['stock']['properties']['warehouse'];

      print_r($a);
      echo $a['type'];

   ?>

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.