What is JSON

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

The above description is taken directly from https://json.org, to put it bluntly, JSON is a representation of data using name/value pairs or an ordered list of values.

The below is a sample of data held in the JSON format.


{
  "countries": [
    {
      "country": "United Kingdom",
      "population": 65640000,
      "currency": "GBP",
      "capital": "London"    
    },
    {
      "country": "Spain",
      "population": 46560000,
      "currency": "EUR",
      "capital": "Madrid"    
    },
    {
      "country": "Portugal",
      "population": 10300000,
      "currency": "EUR",
      "capital": "Lisbon"    
    },
    {
      "country": "Germany",
      "population": 82670000,
      "currency": "EUR",
      "capital": "Berlin"    
    },
    {
      "country": "Japan",
      "population": 127000000,
      "currency": "JPY",
      "capital": "Tokyo"    
    },
  ]
}

As of PHP 5.2, we have a native way of handling JSON formatted data with the function json_decode(). By default the "assoc" parameter will be set to FALSE but if you'd like your JSON data as an associative array instead of an object, you can set this to TRUE as seen in the below example which shows both class and array output.


<?php

$json_data = file_get_contents("sample.json");
print_r(json_decode($json_data));
print_r(json_decode($json_data, true));

/*
stdClass Object
(
    [countries] => Array
        (
            [0] => stdClass Object
                (
                    [country] => United Kingdom
                    [population] => 65640000
                    [currency] => GBP
                    [capital] => London
                )

            [1] => stdClass Object
                (
                    [country] => Spain
                    [population] => 46560000
                    [currency] => EUR
                    [capital] => Madrid
                )

            [2] => stdClass Object
                (
                    [country] => Portugal
                    [population] => 10300000
                    [currency] => EUR
                    [capital] => Lisbon
                )

            [3] => stdClass Object
                (
                    [country] => Germany
                    [population] => 82670000
                    [currency] => EUR
                    [capital] => Berlin
                )

            [4] => stdClass Object
                (
                    [country] => Japan
                    [population] => 127000000
                    [currency] => JPY
                    [capital] => Tokyo
                )

        )

)

Array
(
    [countries] => Array
        (
            [0] => Array
                (
                    [country] => United Kingdom
                    [population] => 65640000
                    [currency] => GBP
                    [capital] => London
                )

            [1] => Array
                (
                    [country] => Spain
                    [population] => 46560000
                    [currency] => EUR
                    [capital] => Madrid
                )

            [2] => Array
                (
                    [country] => Portugal
                    [population] => 10300000
                    [currency] => EUR
                    [capital] => Lisbon
                )

            [3] => Array
                (
                    [country] => Germany
                    [population] => 82670000
                    [currency] => EUR
                    [capital] => Berlin
                )

            [4] => Array
                (
                    [country] => Japan
                    [population] => 127000000
                    [currency] => JPY
                    [capital] => Tokyo
                )

        )

)
*/
?>

Working with the data

Now that we've got our data into PHP, we can then manipulate and display it how we want. The below code will use the associative array version of the sample JSON data described above and will output our data into a neat and tidy HTML table.


<?php

echo "<table><tbody><tr><th>Country Name</th><th>Population</th><th>Currency</th><th>Capital</th></tr>\r\n";
foreach (json_decode($json_data, true)["countries"] as $country) {
    echo "<tr>\r\n";
    echo "<td>" . $country["country"] . "<td>\r\n";
    echo "<td>" . $country["population"] . "<td>\r\n";
    echo "<td>" . $country["currency"] . "<td>\r\n";
    echo "<td>" . $country["capital"] . "<td>\r\n";
    echo "</tr>\r\n";
}
echo "</tbody></table>\r\n";

?>

The end product, a neatly outputted table ready for use

Country NamePopulationCurrencyCapital
United Kingdom 65640000 GBP London
Spain 46560000 EUR Madrid
Portugal 10300000 EUR Lisbon
Germany 82670000 EUR Berlin
Japan 127000000 JPY Tokyo