Ank
Ank

Reputation: 6270

json parsing in php

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?

Upvotes: 0

Views: 1497

Answers (3)

Ryan
Ryan

Reputation: 14649

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'];

   ?>

Upvotes: 0

Tadeck
Tadeck

Reputation: 137360

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.

Upvotes: 4

Eswar Rajesh Pinapala
Eswar Rajesh Pinapala

Reputation: 4911

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>";
}

Upvotes: 0

Related Questions