How To Parse JSON in PHP – With Examples

Whether you have JSON in a .json file, from an API or are passing data from PHP controllers to a view, then PHP is ready. Native PHP has got two helpful functions, json_encode & json_decode. The function we are going to talk about today is json_decode which essentially lets you parse JSON into a usable format.

Firstly, we are going to need some JSON. Credit to Mocakroo for random data generation!

JSON

[{
  "id": 1,
  "first_name": "Bertrando",
  "last_name": "Pedrollo",
  "email": "bpedrollo0@homestead.com",
  "gender": "Male",
  "ip_address": "62.137.20.86"
}, {
  "id": 2,
  "first_name": "Pier",
  "last_name": "Winkworth",
  "email": "pwinkworth1@mit.edu",
  "gender": "Female",
  "ip_address": "158.139.30.200"
}, {
  "id": 3,
  "first_name": "Joyous",
  "last_name": "Glascott",
  "email": "jglascott2@smh.com.au",
  "gender": "Female",
  "ip_address": "146.147.52.106"
}, {
  "id": 4,
  "first_name": "Daniela",
  "last_name": "Hawes",
  "email": "dhawes3@timesonline.co.uk",
  "gender": "Female",
  "ip_address": "148.153.203.134"
}]

Parsing JSON with json_encode

Parse JSON to Object

So, we have our JSON, how do we decode it if it’s contained in a string variable? Let’s see the following code for instance –

$json = '[{ "id": 1, "first_name": "Bertrando", "last_name": "Pedrollo", "email": "bpedrollo0@homestead.com", "gender": "Male", "ip_address": "62.137.20.86" }, { "id": 2, "first_name": "Pier", "last_name": "Winkworth", "email": "pwinkworth1@mit.edu", "gender": "Female", "ip_address": "158.139.30.200" }, { "id": 3, "first_name": "Joyous", "last_name": "Glascott", "email": "jglascott2@smh.com.au", "gender": "Female", "ip_address": "146.147.52.106" }, { "id": 4, "first_name": "Daniela", "last_name": "Hawes", "email": "dhawes3@timesonline.co.uk", "gender": "Female", "ip_address": "148.153.203.134" }]'; 

$decodedJson = json_decode($json); 

var_dump($decodedJson);

After decoding the JSON with a basic function call to json_decode, we dump the data into the page. This will give us an array of object(stdClass) see below –

Output

array(4) { 
[0]=> object(stdClass)#1 (6) { ["id"]=> int(1) ["first_name"]=> string(9) "Bertrando" ["last_name"]=> string(8) "Pedrollo" ["email"]=> string(24) "bpedrollo0@homestead.com" ["gender"]=> string(4) "Male" ["ip_address"]=> string(12) "62.137.20.86" } 

[1]=> object(stdClass)#2 (6) { ["id"]=> int(2) ["first_name"]=> string(4) "Pier" ["last_name"]=> string(9) "Winkworth" ["email"]=> string(19) "pwinkworth1@mit.edu" ["gender"]=> string(6) "Female" ["ip_address"]=> string(14) "158.139.30.200" } 

[2]=> object(stdClass)#3 (6) { ["id"]=> int(3) ["first_name"]=> string(6) "Joyous" ["last_name"]=> string(8) "Glascott" ["email"]=> string(21) "jglascott2@smh.com.au" ["gender"]=> string(6) "Female" ["ip_address"]=> string(14) "146.147.52.106" } 

[3]=> object(stdClass)#4 (6) { ["id"]=> int(4) ["first_name"]=> string(7) "Daniela" ["last_name"]=> string(5) "Hawes" ["email"]=> string(25) "dhawes3@timesonline.co.uk" ["gender"]=> string(6) "Female" ["ip_address"]=> string(15) "148.153.203.134" } 
}

Now, after seeing this output, we know we can start to get data from the decodedJson variable with more object-like-syntax. See below

echo $decodedJson[0]->first_name . " " . $decodedJson[0]->last_name;
echo "<br />";
echo $decodedJson[1]->first_name . " " . $decodedJson[1]->last_name;

//Outputs
// Bertrando Pedrollo
// Pier Winkworth

Parsing JSON to Associative Array

Now, what if we want to parse the JSON into an associative array rather than a stdClass? json_encode() allows additional parameters, one of which is bool $assoc = FALSE. All we need to do is pass in True.

See the example below that uses this functionality and dumps it to the page.

$json = '[{ "id": 1, "first_name": "Bertrando", "last_name": "Pedrollo", "email": "bpedrollo0@homestead.com", "gender": "Male", "ip_address": "62.137.20.86" }, { "id": 2, "first_name": "Pier", "last_name": "Winkworth", "email": "pwinkworth1@mit.edu", "gender": "Female", "ip_address": "158.139.30.200" }, { "id": 3, "first_name": "Joyous", "last_name": "Glascott", "email": "jglascott2@smh.com.au", "gender": "Female", "ip_address": "146.147.52.106" }, { "id": 4, "first_name": "Daniela", "last_name": "Hawes", "email": "dhawes3@timesonline.co.uk", "gender": "Female", "ip_address": "148.153.203.134" }]';

$decodedJson = json_decode($json, true);

var_dump($decodedJson);

Output

array(4) { [0]=> array(6) { ["id"]=> int(1) ["first_name"]=> string(9) "Bertrando" ["last_name"]=> string(8) "Pedrollo" ["email"]=> string(24) "bpedrollo0@homestead.com" ["gender"]=> string(4) "Male" ["ip_address"]=> string(12) "62.137.20.86" } [1]=> array(6) { ["id"]=> int(2) ["first_name"]=> string(4) "Pier" ["last_name"]=> string(9) "Winkworth" ["email"]=> string(19) "pwinkworth1@mit.edu" ["gender"]=> string(6) "Female" ["ip_address"]=> string(14) "158.139.30.200" } [2]=> array(6) { ["id"]=> int(3) ["first_name"]=> string(6) "Joyous" ["last_name"]=> string(8) "Glascott" ["email"]=> string(21) "jglascott2@smh.com.au" ["gender"]=> string(6) "Female" ["ip_address"]=> string(14) "146.147.52.106" } [3]=> array(6) { ["id"]=> int(4) ["first_name"]=> string(7) "Daniela" ["last_name"]=> string(5) "Hawes" ["email"]=> string(25) "dhawes3@timesonline.co.uk" ["gender"]=> string(6) "Female" ["ip_address"]=> string(15) "148.153.203.134" } }

Now, if we call the $jsonDecoded variable just as you would with an associative array, we will get the values from the parsed-associative array.

echo $decodedJson[0]["first_name"] . " " . $decodedJson[0]["last_name"]; 
echo "<br />";
echo $decodedJson[1]["first_name"] . " " . $decodedJson[1]["last_name"];


//Outputs
// Bertrando Pedrollo
// Pier Winkworth

References

Throughout the examples, within this article, the native PHP function we have used has some great documentation. Below is the link direct to the PHP docs and is well worth a read.

json_decode Documentation

Summary

Someone once told me that PHP is awful, but little native functions like these make using PHP a dream. One line of code to solve all your JSON parsing problems. It can’t get any better than that!

Posted in PHP

Leave a Reply

Your email address will not be published.