How to parse JSON file in PHP?

JSON or JavaScript Object Notation is a lightweight data-interchange format. Developers often need to work with JSON to load and manipulate JSON feeds from other sites.

Lots of sites share data in JSON format. So obviously you need to know how to work with JSON in various programming language.

In this tutorial, I will show you how to parse JSON from an external JSON file using PHP programming language.

Example of parsing external JSON in PHP from external JSON file

I have almost always given you an example tutorial so that it will be easier for you to understand and this time also I am going to do this. It will be best to show you with an easy example how to parse JSON objects using PHP.

Suppose we want to parse JSON object from an external JSON file that is in the URL The JSON file contains the below JSON code:

    "Feel": {
    "Suncovered": {
    "Weather": {

Now I am going to parse it using PHP code. Below is the PHP code to parse the JSON file:

$jsonfile = file_get_contents("");

$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($jsonfile, TRUE)),

foreach ($jsonIterator as $key => $value) {
    if(is_array($value)) {
        echo "$key:\n";
  else {
        echo "$key => $value\n";

If you want to run the code on your computer then you can create new JSON file with the same contents and keep it on your computer and then give the path instead of the external URL.

Now run the code on your server. You will see the result that looks like below:

Feel: status => warm Suncovered: status => false Weather: cloudy => yes Temperature => 56 humidity => 10 temperature_fall => 0.045 wind => 22

So how was that? Is that not so interesting? This tutorial may be so useful to you if you working with JSON.

Leave a Reply

Your email address will not be published. Required fields are marked *