PHP JSON Parsing

Tutorial Difficulty Level    

JSON stands for JavaScript Object Notation. JSON is a standard lightweight data-interchange format which is quick and easy to parse and generate (a data-interchange format is a text format which is used to interchange or exchange data between different platforms and operating systems).

JSON is the most popular and lightweight data-interchange format for web applications.

JSON, like XML, is a text-based format that’s easy to write and easy to understand for both humans and computers, but unlike XML, JSON data structures occupy less bandwidth than their XML versions. JSON is based on two basic structures:

  • Object: This is defined as a collection of key/value pairs (i.e. key:value). Each object begins with a left curly bracket { and ends with a right curly bracket }. Multiple key/value pairs are separated by a comma ,.
  • Array: This is defined as an ordered list of values. An array begins with a left bracket [ and ends with a right bracket ]. Values are separated by a comma ,.

In JSON, keys are always strings, while the value can be a stringnumbertrue or falsenull or even an object or an array. Strings must be enclosed in double quotes " and can contain escape characters such as \n\t and \. A JSON object may look like this:

Whereas an example of JSON array would look something like this:

Parsing JSON with PHP

JSON data structures are very similar to PHP arrays. PHP has built-in functions to encode and decode JSON data. These functions are json_encode() and json_decode(), respectively. Both functions only works with UTF-8 encoded string data.

Encoding JSON Data in PHP

In PHP the json_encode() function is used to encode a value to JSON format. The value being encoded can be any PHP data type except a resource, like a database or file handle. The below example demonstrates how to encode a PHP associative array into a JSON object:

The output of the above example will look like this:

Similarly, you can encode the PHP indexed array into a JSON array, like this:

The output of the above example will look like this:

You can also force json_encode() function to return an PHP indexed array as JSON object by using the JSON_FORCE_OBJECT option, as shown in the example below:

The output of the above example will look like this:

As you can see in the above examples a non-associative array can be encoded as array or object. However, an associative array always encoded as object.

Decoding JSON Data in PHP

Decoding JSON data is as simple as encoding it. You can use the PHP json_decode() function to convert the JSON encoded string into appropriate PHP data type. The following example demonstrates how to decode or convert a JSON object to PHP object.

The output of the above example will look something like this:

By default the json_decode() function returns an object. However, you can optionally specify a second parameter $assoc which accepts a boolean value that when set as true JSON objects are decoded into associative arrays. It is false by default. Here’s an example:

The output of the above example will look something like this:

Now let’s check out an example that will show you how to decode the JSON data and access individual elements of the JSON object or array in PHP.

You can also loop through the decoded data using foreach() loop, like this:

Extracting Values from Nested JSON Data in PHP

JSON objects and arrays can also be nested. A JSON object can arbitrarily contains other JSON objects, arrays, nested arrays, arrays of JSON objects, and so on. The following example will show you how to decode a nested JSON object and print all its values in PHP.