<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Decodes a JSON string</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="ref.json.html">JSON Functions</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.json-encode.html">json_encode</a></div> <div class="up"><a href="ref.json.html">JSON Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.json-decode" class="refentry"> <div class="refnamediv"> <h1 class="refname">json_decode</h1> <p class="verinfo">(PHP 5 >= 5.2.0, PHP 7, PECL json >= 1.2.0)</p><p class="refpurpose"><span class="refname">json_decode</span> — <span class="dc-title">Decodes a JSON string</span></p> </div> <div class="refsect1 description" id="refsect1-function.json-decode-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>json_decode</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$json</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$assoc</code><span class="initializer"> = <strong><code>FALSE</code></strong></span></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$depth</code><span class="initializer"> = 512</span></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$options</code><span class="initializer"> = 0</span></span> ]]] )</div> <p class="para rdfs-comment"> Takes a JSON encoded string and converts it into a PHP variable. </p> </div> <div class="refsect1 parameters" id="refsect1-function.json-decode-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">json</code></dt> <dd> <p class="para"> The <code class="parameter">json</code> <span class="type"><a href="language.types.string.html" class="type string">string</a></span> being decoded. </p> <p class="para"> This function only works with UTF-8 encoded strings. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> PHP implements a superset of JSON as specified in the original <a href="http://www.faqs.org/rfcs/rfc7159" class="link external">» RFC 7159</a>. </p> </p></blockquote> </dd> <dt> <code class="parameter">assoc</code></dt> <dd> <p class="para"> When <strong><code>TRUE</code></strong>, returned <span class="type"><a href="language.types.object.html" class="type object">object</a></span>s will be converted into associative <span class="type"><a href="language.types.array.html" class="type array">array</a></span>s. </p> </dd> <dt> <code class="parameter">depth</code></dt> <dd> <p class="para"> User specified recursion depth. </p> </dd> <dt> <code class="parameter">options</code></dt> <dd> <p class="para"> Bitmask of <strong><code>JSON_BIGINT_AS_STRING</code></strong> (enabled by default), <strong><code>JSON_OBJECT_AS_ARRAY</code></strong>, <strong><code>JSON_THROW_ON_ERROR</code></strong>. The behaviour of these constants is described on the <a href="json.constants.html" class="link">JSON constants</a> page. </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.json-decode-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the value encoded in <code class="parameter">json</code> in appropriate PHP type. Values <em>true</em>, <em>false</em> and <em>null</em> are returned as <strong><code>TRUE</code></strong>, <strong><code>FALSE</code></strong> and <strong><code>NULL</code></strong> respectively. <strong><code>NULL</code></strong> is returned if the <code class="parameter">json</code> cannot be decoded or if the encoded data is deeper than the recursion limit. </p> </div> <div class="refsect1 examples" id="refsect1-function.json-decode-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4642"> <p><strong>Example #1 <span class="function"><strong>json_decode()</strong></span> examples</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$json </span><span style="color: #007700">= </span><span style="color: #DD0000">'{"a":1,"b":2,"c":3,"d":4,"e":5}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> object(stdClass)#1 (5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) } array(5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) } </pre></div> </div> </div> <div class="example" id="example-4643"> <p><strong>Example #2 Accessing invalid object properties</strong></p> <div class="example-contents"><p> 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. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$json </span><span style="color: #007700">= </span><span style="color: #DD0000">'{"foo-bar": 12345}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$obj </span><span style="color: #007700">= </span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">);<br />print </span><span style="color: #0000BB">$obj</span><span style="color: #007700">->{</span><span style="color: #DD0000">'foo-bar'</span><span style="color: #007700">}; </span><span style="color: #FF8000">// 12345<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-4644"> <p><strong>Example #3 common mistakes using <span class="function"><strong>json_decode()</strong></span></strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">// the following strings are valid JavaScript but not valid JSON<br /><br />// the name and value must be enclosed in double quotes<br />// single quotes are not valid <br /></span><span style="color: #0000BB">$bad_json </span><span style="color: #007700">= </span><span style="color: #DD0000">"{ 'bar': 'baz' }"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$bad_json</span><span style="color: #007700">); </span><span style="color: #FF8000">// null<br /><br />// the name must be enclosed in double quotes<br /></span><span style="color: #0000BB">$bad_json </span><span style="color: #007700">= </span><span style="color: #DD0000">'{ bar: "baz" }'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$bad_json</span><span style="color: #007700">); </span><span style="color: #FF8000">// null<br /><br />// trailing commas are not allowed<br /></span><span style="color: #0000BB">$bad_json </span><span style="color: #007700">= </span><span style="color: #DD0000">'{ bar: "baz", }'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$bad_json</span><span style="color: #007700">); </span><span style="color: #FF8000">// null<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-4645"> <p><strong>Example #4 <code class="parameter">depth</code> errors</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Encode the data.<br /></span><span style="color: #0000BB">$json </span><span style="color: #007700">= </span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(<br /> array(<br /> </span><span style="color: #0000BB">1 </span><span style="color: #007700">=> array(<br /> </span><span style="color: #DD0000">'English' </span><span style="color: #007700">=> array(<br /> </span><span style="color: #DD0000">'One'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'January'<br /> </span><span style="color: #007700">),<br /> </span><span style="color: #DD0000">'French' </span><span style="color: #007700">=> array(<br /> </span><span style="color: #DD0000">'Une'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'Janvier'<br /> </span><span style="color: #007700">)<br /> )<br /> )<br />);<br /><br /></span><span style="color: #FF8000">// Define the errors.<br /></span><span style="color: #0000BB">$constants </span><span style="color: #007700">= </span><span style="color: #0000BB">get_defined_constants</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$json_errors </span><span style="color: #007700">= array();<br />foreach (</span><span style="color: #0000BB">$constants</span><span style="color: #007700">[</span><span style="color: #DD0000">"json"</span><span style="color: #007700">] as </span><span style="color: #0000BB">$name </span><span style="color: #007700">=> </span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br /> if (!</span><span style="color: #0000BB">strncmp</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">, </span><span style="color: #DD0000">"JSON_ERROR_"</span><span style="color: #007700">, </span><span style="color: #0000BB">11</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">$json_errors</span><span style="color: #007700">[</span><span style="color: #0000BB">$value</span><span style="color: #007700">] = </span><span style="color: #0000BB">$name</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #FF8000">// Show the errors for different depths.<br /></span><span style="color: #007700">foreach (</span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">) as </span><span style="color: #0000BB">$depth</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">, </span><span style="color: #0000BB">$depth</span><span style="color: #007700">));<br /> echo </span><span style="color: #DD0000">'Last error: '</span><span style="color: #007700">, </span><span style="color: #0000BB">$json_errors</span><span style="color: #007700">[</span><span style="color: #0000BB">json_last_error</span><span style="color: #007700">()], </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> array(1) { [1]=> array(2) { ["English"]=> array(2) { [0]=> string(3) "One" [1]=> string(7) "January" } ["French"]=> array(2) { [0]=> string(3) "Une" [1]=> string(7) "Janvier" } } } Last error: JSON_ERROR_NONE NULL Last error: JSON_ERROR_DEPTH </pre></div> </div> </div> <div class="example" id="example-4646"> <p><strong>Example #5 <span class="function"><strong>json_decode()</strong></span> of large integers</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$json </span><span style="color: #007700">= </span><span style="color: #DD0000">'{"number": 12345678901234567890}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">, </span><span style="color: #0000BB">512</span><span style="color: #007700">, </span><span style="color: #0000BB">JSON_BIGINT_AS_STRING</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> object(stdClass)#1 (1) { ["number"]=> float(1.2345678901235E+19) } object(stdClass)#1 (1) { ["number"]=> string(20) "12345678901234567890" } </pre></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.json-decode-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The JSON spec is not JavaScript, but a subset of JavaScript. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> In the event of a failure to decode, <span class="function"><a href="function.json-last-error.html" class="function">json_last_error()</a></span> can be used to determine the exact nature of the error. </p> </p></blockquote> </div> <div class="refsect1 changelog" id="refsect1-function.json-decode-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>7.1.0</td> <td> An empty JSON key ("") can be encoded to the empty object property instead of using a key with value <em>_empty_</em>. </td> </tr> <tr> <td>7.0.0</td> <td> Rejected RFC 7159 incompatible number formats - top level (07, 0xff, .1, -.1) and all levels ([1.], [1.e1]) </td> </tr> <tr> <td>7.0.0</td> <td> An empty PHP string or value that after casting to string is an empty string (<em>NULL</em>, <em>FALSE</em>) results in JSON syntax error. </td> </tr> <tr> <td>5.6.0</td> <td> Invalid non-lowercased variants of the <em>true</em>, <em>false</em> and <em>null</em> literals are no longer accepted as valid input, and will generate warnings. </td> </tr> <tr> <td>5.4.0</td> <td> The <code class="parameter">options</code> parameter was added. </td> </tr> <tr> <td>5.3.0</td> <td> Added the optional <code class="parameter">depth</code>. The default recursion depth was increased from 128 to 512 </td> </tr> <tr> <td>5.2.3</td> <td> The nesting limit was increased from 20 to 128 </td> </tr> <tr> <td>5.2.1</td> <td> Added support for JSON decoding of basic types. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 seealso" id="refsect1-function.json-decode-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><span class="function"><a href="function.json-encode.html" class="function" rel="rdfs-seeAlso">json_encode()</a> - Returns the JSON representation of a value</span></li> <li class="member"><span class="function"><a href="function.json-last-error.html" class="function" rel="rdfs-seeAlso">json_last_error()</a> - Returns the last error occurred</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="ref.json.html">JSON Functions</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.json-encode.html">json_encode</a></div> <div class="up"><a href="ref.json.html">JSON Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>