1

I've a json structure :

{
    "Photos":{
        "Photo":[
            {
                "ID" : 111,
                 "type" : "JPEG",
                "URL": "blabla"  
            },
            {
                "ID": 222,
                "type": "JPG",
                "URL": "blaaaaaaaaa"
            }
        ]
    }
}

Am attempting to use php-jsonpath (By stefan Goessner : http://goessner.net/articles/JsonPath) to fetch node (photo) where ID == 222. And then, if found, insert a new data (array) into that node. Such that final output becomes :

{
    "Photos":{
        "Photo":[
            {
                "ID" : 111,
                 "type" : "JPEG",
                "URL": "blabla"  
            },
            {
                "ID": 222,
                "type": "JPG",
                "URL": "blaaaaaaaaa",
                "New_data" : {"CROP": "5x7", "Pixel": "none"}
            }
        ]
    }
}

currently i have this query string:

$parser = new Services_JSON(SERVICE_JSON_LOOSE_TYPE);
$jsonobj = $parser->decode(file_get_contents("filename.json", true));

$result = jsonPath($jsonobj , "$..Photo[?(@.ID == 222)]");   //is this expression wrong ?

i am only expectant of the node with the said ID (222) as returned $result, so that i can do this :

if($result != false){
     $result[] = array("New_data" => array("CROP" => 5x7, "Pixel" => "none"));
}

Unfortunately i go the error message :

Notice: Array to string conversion in C:\Server\wamp\www\MyCMS\jsonpath.php(119) 

What sin am i commiting ? and how do i get around it ? any code snippet (if necessary) will be appreciated. thanks !

Fuzzyma
  • 7,619
  • 6
  • 28
  • 60
Osagie Odigie
  • 281
  • 1
  • 3
  • 13

1 Answers1

0

You can use loop for iterating values-

       $list = array();
        foreach ($_POST['Photos']['Photo'] as $key => $value) {
                    $list[] = $value;
            }
Rishabh
  • 167
  • 2
  • 13
  • thanks, but i cant seem to relate ur code snippette with my post. i needed to obtain the photo node first before inserting any value. how can i do that ? – Osagie Odigie Jan 20 '15 at 12:43