Today, In this blog we are going to learn “How to decode JSON data and accessing the results in PHP”. JSON is not an array, an object, or a data structure. JSON is a text-based serialization format – so a fancy string, but still just a string. Decode it in PHP by using json_decode(). $data = json_decode($json); Suppose we have this JSON File. { "type": "foo", "name": "bar", "property": [ { "id": "502", "type": "property1" }, { "id": "504", "type": "property2" }, { "id": "506", "type": "property3" }, { "id": "508", "type": "property4" } ] } How do we decode this JSON data in PHP and access the resulting data? Accessing object properties You access the properties of one of these objects the same way you would for the public non-static properties of any other object, e.g. $object->property. $json_data = ' { "type": "foo", "name": "bar" }'; $data= json_decode($json_data); echo $data->type; //foo echo $data->name; //bar Accessing array elements You access the elements of one of these arrays the same way you would for any other array, e.g. $array[0]. $json_data = ' [ "property1", "property2", "property3", "property4" ]'; $data= json_decode($json_data ); echo $data[0]; //property1 echo $data[1]; //property2 echo $data[2]; //property3 echo $data[3]; //property4 Iterate it with foreach loop. foreach ($property as $propertys) { echo $propertys, "\n"; } Output: property1 property2 property3 property4 You May Also Like: Limit number of login attempt using PHP & MySQL Take Image Snapshot from a webcam with Jquery and HTML PHP include() Vs require() Is PHP in 2019 still a relevant language Passing true as the second argument to json_decode() When you do this, instead of objects you’ll get associative arrays – arrays with strings for keys. Again you access the elements thereof as usual, e.g. $array[‘key’]. $json_data = ' { "type": "foo", "name": "bar", "property": [ { "id": "502", "type": "property1" }, { "id": "504", "type": "property2" }, { "id": "506", "type": "property3" }, { "id": "508", "type": "property4" } ] }'; $data= json_decode($json_data , true); echo $data['property'][2]['type']; //property3 Don’t know how the data is structured Read the documentation for whatever it is you’re getting the JSON from. Look at the JSON – where you see curly brackets {} expect an object, where you see square brackets [] expect an array. Hit the decoded data with a print_r(): $json = ' { "type": "donut", "name": "Cake", "toppings": [ { "id": "5002", "type": "Glazed" }, { "id": "5006", "type": "Chocolate with Sprinkles" }, { "id": "5004", "type": "Maple" } ] }'; $json_data = '{ "type": "foo", "name": "bar", "property": [ { "id": "502", "type": "property1" }, { "id": "504", "type": "property2" }, { "id": "506", "type": "property3" }, { "id": "508", "type": "property4" } ] }'; $data= json_decode($json_data ); print_r($data); and check the output: stdClass Object ( [type] => foo [name] => bar [property] => Array ( [0] => stdClass Object ( [id] => 502 [type] => property1 ) [1] => stdClass Object ( [id] => 504 [type] => property2 ) [2] => stdClass Object ( [id] => 506 [type] => property3 ) [3] => stdClass Object ( [id] => 508 [type] => property4 ) ) ) It’ll tell you where you have objects, where you have arrays, along with the names and values of their members. Few Points to remember: json_decode requires the string to be a valid json else it will return NULL. In the event of a failure to decode, json_last_error() can be used to determine the exact nature of the error. Make sure you pass in utf8 content, or json_decode may error out and just return a NULLvalue. Share this:Click to share on Twitter (Opens in new window)Click to share on Facebook (Opens in new window)Click to print (Opens in new window)Click to share on LinkedIn (Opens in new window)Click to share on Telegram (Opens in new window)Click to share on WhatsApp (Opens in new window)Like this:Like Loading... Post navigation Limit number of login attempt using PHP & MySQL Limit the number of words in a div using HTML and PHP