Decoding JSON in PHP

To decode a JSON string or file in PHP, you can use the json_decode($json, $assoc, $depth, $options) function. The first parameter specifies the string to be decoded. The second optional parameter sets whether the returned object should be converted to an associative array. By default, the json_decode() function recursively decodes JSON with a depth of 512. You can set a different recursion depth in the third parameter. If the JSON cannot be decoded or the nesting depth of the data is greater than the recursion limit, the decode function will return NULL. To encode PHP objects to JSON, you can use the json_encode($value) function. In this PHP Decode JSON example, we use the json_decode() function to convert a JSON string into a PHP object using default values. Click Execute to run the PHP JSON Decode Example online and see the result.
Decoding JSON in PHP Execute
<?php
$json = '{"Leo":25,"Alice":37,"Tim":43}';

print_r (json_decode($json));
?>
Updated: Viewed: 5403 times

What is PHP?

PHP is a server-side scripting language that is widely used for web development programming. It can be embedded into HTML and executed on the server side to create dynamic web pages and web-based applications. PHP has a large developer community, extensive documentation, and many open-source projects, making it easy for developers to learn and use. PHP is cross-platform and can run on various operating systems like Linux, Windows, and macOS.

What is JSON?

JSON (JavaScript Object Notation) is a standard text-based data interchange format that is based on the JavaScript object syntax. JSON is widely used for data exchange between applications written in many programming languages, including JavaScript, PHP, Java, C++, C#, Go, Python, and many more.

How to decode JSON in PHP?

In PHP, the json_decode() function is used to decode JSON strings into PHP objects. The result object can be any PHP data type except for resource pointers such as a database or file descriptor. Following is the syntax of the json_decode() function:

PHP json_decode() Syntax
json_decode(string, assoc, depth, options)

Where:
  • string: specifies the value to be decoded. It only works with UTF-8 encoded strings.
  • assoc (Optional): specifies a boolean value. If set to "true", the returned object will be converted to an associative array. If set to "false", it returns an object. The default value is "false".
  • depth (Optional): specifies the recursion depth. The default JSON decoding recursion depth is 512.
  • options (Optional): specifies a bitmask that determines how JSON objects will be converted to PHP objects. The decode function supports the following bitmasks: JSON_BIGINT_AS_STRING, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR. You can learn more about JSON decoding bitmasks in the PHP documentation.
PHP json_decode() Example
<?php
$json = '{"Leo":25,"Alice":37,"Tim":43}';

print_r (json_decode($json));
?>

#output: stdClass Object
#(
#    [Leo] => 25
#    [Alice] => 37
#    [Tim] => 43
#)

PHP JSON Decode Function Examples

The following are examples of using the json_decode() function in PHP:

Decoding JSON string

The following is an example of decoding a JSON string into an object using json_decode():

PHP Decoding JSON String Example
<?php
$json = '{"name": "Leo", "age": 26, "city": "Madrid"}';

print_r (json_decode($json));
?>

#output: stdClass Object
#(
#    [name] => Leo
#    [age] => 26
#    [city] => Madrid
#)

Decoding JSON array

The following is an example of decoding a JSON array into an object using json_decode():

PHP Decoding JSON Array Example
<?php
$json = '["PHP", "JSON", "Example"]';

print_r (json_decode($json));
?>

#output: Array
#(
#    [0] => PHP
#    [1] => JSON
#    [2] => Example
#)

Decoding a JSON containing nested objects

The following is an example of decoding JSON containing nested objects into a PHP object using json_decode():

PHP Decoding JSON with Nested Objects Example
<?php
$json = '{"person": {"name": "Leo", "age": 26, "city": "Madrid"}}';

print_r (json_decode($json));
?>

#output: stdClass Object
#(
#    [person] => stdClass Object
#        (
#            [name] => Leo
#            [age] => 26
#            [city] => Madrid
#        )
#
#)

How to encode PHP object to JSON string?

To encode a PHP object to JSON string, you can use the json_encode() function.

PHP Encoding Example
<?php
$json = array(
  'Jack' => 25,
  'Stasy' => 57,
  'Tim' => 13
);

echo json_encode($json);
?>

#output: {"Jack":25,"Stasy":57,"Tim":13}

See also