AppDividend
Latest Code Tutorials

PHP json_decode Example | Decode JSON Data in PHP Tutorial

0

PHP json_decode Example | Decode JSON Data in PHP Tutorial is today’s topic. PHP json_decode function takes the JSON encoded string and converts it into a PHP variable. JSON stands for JavaScript Object Notation. JSON is the standard lightweight data-interchange format which 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.

If you want to learn more about PHP then check out this course PHP for Beginners – Become a PHP Master – CMS Project

PHP json_decode Example

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. Strings must be enclosed in the double quotes and can contain escape characters such as \n, \t, and \

JSON data structures are very similar to the 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. You can use a PHP json_decode() function to convert a JSON encoded string into the appropriate PHP data type. 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 − It is the encoded string which must be the UTF-8 encoded data.

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

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 setting 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

 

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

 

Finally, PHP json_decode Example | Decode JSON Data in PHP Tutorial is over.

Leave A Reply

Your email address will not be published.

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