AppDividend
Latest Code Tutorials

PHP json_decode: How to Decode JSON Data in PHP

JSON stands for JavaScript Object Notation. JSON is the standard lightweight data-interchange format that is quick and easy to parse and generate. The JSON, like XML, is the text-based format that’s easy to write and easy to understand for both computers and humans, but unlike the XML, JSON data structures occupy less bandwidth than their XML versions.

PHP json_decode

PHP json_decode() is a built-in function that takes the JSON encoded string and converts it into a PHP variable. The json_decode() function takes json_string, assoc, depth, and options as arguments and returns the PHP object. The json_decode() function converts a JSON encoded string into the appropriate data type.

In JSON, keys are always strings, while a value can be a string, number, true or false, null or even the object or an array. Furthermore, strings must be enclosed in the double quotes and contain escape characters such as \n, \t, and \

JSON data structures are very similar to PHP arrays. PHP programming language has built-in functions to encode and decode the JSON data. The most common JSON PHP functions are json_encode and json_decode, respectively. Both functions only work with UTF-8 encoded string data.

Decoding JSON data is as simple as encoding it. We will see how to decode or convert a JSON object to a PHP object.

Syntax

The syntax of json_decode() function is following.

json_decode ( string $json [, bool $assoc = FALSE [, int $depth = 512 [, int $options = 0 ]]] )
  1. json_string − The encoded string must be the UTF-8 encoded data.

  2. assoc − It is the boolean type parameter; when set to TRUE, returned, the objects will be converted into the associative arrays.
  3. depth − It is the integer type parameter that specifies the recursion depth
  4. options − It is the integer type bitmask of JSON decode, JSON_BIGINT_AS_STRING is supported.

Example

See the following example of decoding json data in php.

<?php

// app.php

$json = '{"website": 19, 
          "author": 21, 
          "category": 46}';

print_r(json_decode($json));

See the below output.

PHP json_decode Example

By default the json_decode() function returns the object.

However, you can optionally specify the second parameter assoc, which accepts the boolean value that, when set as true, the JSON objects are decoded into the associative arrays. It is false by default. See the following example.

<?php

// app.php

$json = '{"website": 19, 
          "author": 21, 
          "category": 46}';

var_dump(json_decode($json, true));

See the below output.

Decode JSON Data in PHP Tutorial

Accessing invalid object properties

Accessing elements within an object that contain characters not permitted under PHP’s naming convention (e.g., the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe. See the below example.

<?php

// app.php

$json = '{"app-dividend": 192146}';

$obj = json_decode($json);
print $obj->{'app-dividend'};

See the below output.

Accessing invalid object properties

PHP json_decode() of large integers

See the below example, where the value is a much larger integer.

<?php

// app.php

$json = '{"number": 1234567890123456789}';

var_dump(json_decode($json));
var_dump(json_decode($json, false, 512, JSON_BIGINT_AS_STRING));

See the below output.

json_decode() of large integers

That’s it for this example.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.