# PHP json_decode Function | Decode JSON String

The PHP json_decode is an inbuilt Function in PHP that converts a JSON String to PHP Object. For example, it can convert a JSON String to an array in PHP. In this article, we will discuss the PHP json_decode Function. Also, we will discuss a few examples of using it.

Also, in order to convert a PHP object to a JSON String, you can use the PHP json_encode Function.

## Syntax

mixed json_deocde(\$json, \$assoc = false, \$depth = 512, \$options = 0)

## Parameters

The PHP json_decode Function expects one mandatory and three optional parameters. The description of the parameters is given below as follows:

• \$json: The first parameter is the mandatory JSON which the function will decode.
• \$assoc: When the value of \$assoc is true, the function will return an associative array. However, by default the value is false and the function returns a class object.
• \$depth: User specified recursion depth. The default value to this parameter is 512.
• \$options: You can also pass optional bitmasking options to the json_encode Function in PHP. The behavior of the constants is defined on JSON Constants Page.

## Return Value

The PHP json_decode Function returns the encoded JSON string to the corresponding PHP data type. However, it returns NULL on failure.

## Examples

#### Example 1: JSON to Object

By default the function returns a class object.

```<?php
\$jsonString = '{"Users":[{"First":"Chandler","Last":"Bing"},{"First":"Rachael","Last":"Green"},{"First":"Ross","Last":"Geller"}]}';

print_r(json_decode(\$jsonString));
?>```

The output of the above code snippet will be as follows:

`stdClass Object     (         [Users] => Array             (                  => stdClass Object                     (                         [First] => Chandler                         [Last] => Bing                     )                  => stdClass Object                     (                         [First] => Rachael                         [Last] => Green                     )                  => stdClass Object                     (                         [First] => Ross                         [Last] => Geller                     )             )     )`

#### Example 2: JSON to PHP Array

Alternatively, you can also convert the JSON value to an associative array by passing true in the second parameter.

```<?php
\$jsonString = '{"Users":[{"First":"Chandler","Last":"Bing"},{"First":"Rachael","Last":"Green"},{"First":"Ross","Last":"Geller"}]}';

print_r(json_decode(\$jsonString, true));
?>```
`Array     (         [Users] => Array             (                  => Array                     (                         [First] => Chandler                         [Last] => Bing                     )                  => Array                     (                         [First] => Rachael                         [Last] => Green                     )                  => Array                     (                         [First] => Ross                         [Last] => Geller                     )             )     )`

## Conclusion

In this article, we discussed the PHP json_decode Function. It is used to convert a JSON String to PHP data type. You can use the PHP json_encode Function to convert a PHP data type to a valid JSON.
{"ft_lang_label":"__label__en","ft_lang_prob":0.66313195,"math_prob":0.6381941,"size":2841,"snap":"2020-45-2020-50","text_gpt3_token_len":699,"char_repetition_ratio":0.13323934,"word_repetition_ratio":0.1367713,"special_character_ratio":0.27384722,"punctuation_ratio":0.16247582,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9545605,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-27T01:40:48Z\",\"WARC-Record-ID\":\"<urn:uuid:99ced020-7c72-46dc-adcb-d1121da9ea69>\",\"Content-Length\":\"80044\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:772869a7-324a-43a1-ade7-deb3fba15e50>\",\"WARC-Concurrent-To\":\"<urn:uuid:197d88c7-3196-4a81-81d6-3981ca624fad>\",\"WARC-IP-Address\":\"104.27.166.77\",\"WARC-Target-URI\":\"https://concatly.com/php-json-decode-function/\",\"WARC-Payload-Digest\":\"sha1:MNCGMFY72OH367H5F2TLI4R4Z3JA4PZ4\",\"WARC-Block-Digest\":\"sha1:URZCU3ZJDKNUWKEXNFYQZFZPPWNJN56I\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107892710.59_warc_CC-MAIN-20201026234045-20201027024045-00717.warc.gz\"}"}