JSON to PHP Using json_decode

PHP's json_decode function takes a JSON string and converts it into a PHP variable. Typically, the JSON data will represent a JavaScript array or object literal which json_decode will convert into a PHP array or object. The following two examples demonstrate, first with an array, then with an object:

$json = '["apple","orange","banana","strawberry"]';
$ar = json_decode($json);
// access first element of $ar array
echo $ar[0]; // apple

By default, objects are converted to standard objects by json_decode:

$json = '{
    "title": "JavaScript: The Definitive Guide",
    "author": "David Flanagan",
    "edition": 6
}';
$book = json_decode($json);
// access title of $book object
echo $book->title; // JavaScript: The Definitive Guide 

Convert JSON String to PHP Array

The json_decode function provides an optional second argument to convert objects to associative arrays. The following uses the object from the previous example and passes true as the second argument. The result, held in $book, is now an array, and its title and other elements can be accessed using array syntax:

// $json same as example object above
// pass true to convert objects to associative arrays
$book = json_decode($json, true);
// access title of $book array
echo $book['title']; // JavaScript: The Definitive Guide

JSON String to Multidimensional Array

The json_decode function can also be used to convert more complex data structures held in JSON strings. The JSON string ($json) in the following example represents an array of objects. That is, the outer level is an array literal whose elements are object literals. By default the result of json_decode will be a numerically indexed array of objects:

$json = '[
    {
        "title": "Professional JavaScript",
        "author": "Nicholas C. Zakas"
    },
    {
        "title": "JavaScript: The Definitive Guide",
        "author": "David Flanagan"
    },
    {
        "title": "High Performance JavaScript",
        "author": "Nicholas C. Zakas"
    }
]';

$books = json_decode($json);
// access property of object in array
echo $books[1]->title; // JavaScript: The Definitive Guide

If we pass true as the second argument to json_decode, the result is a multidimensional array that is numerically indexed at the outer level and associative at the inner level:

// $json same as example object above
// pass true to convert objects to associative arrays
$books = json_decode($json, true);
// numeric/associative array access
echo $books[1]['title']; // JavaScript: The Definitive Guide

Then we use a combination of numeric and associative array syntax to access the desired element in our multidimensional array.

Errors or Unexpected Results with json_decode?

The json_decode function will return null if the string passed to it cannot be converted. For example, the following demonstrates the result when our JSON string contains invalid characters:[1]

$json = "{
    'title': 'JavaScript: The Definitive Guide',
    'author': 'David Flanagan',
    'edition': 6
}";

$book = json_decode($json);
echo $book->title; // Notice: Trying to get property of non-object...
echo json_last_error(); // 4 (JSON_ERROR_SYNTAX)
echo json_last_error_msg(); // unexpected character 

Our attempt to access a property of the object we are expecting results in an error. The PHP functions json_last_error and json_last_error_msg provide some information about errors encountered.[2]

Back to top


  1. Single quotes are not valid in JSON. ^
  2. See the PHP Manual for information on json_last_error. ^