
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>PHP: json_decode - Manual </title>

    <link rel="shortcut icon" href="https://www.php.net/favicon.ico">
    <link rel="search" type="application/opensearchdescription+xml" href="http://php.net/phpnetimprovedsearch.src" title="Add PHP.net search">
    <link rel="alternate" type="application/atom+xml" href="https://www.php.net/releases/feed.php" title="PHP Release feed">
    <link rel="alternate" type="application/atom+xml" href="https://www.php.net/feed.atom" title="PHP: Hypertext Preprocessor">

    <link rel="canonical" href="https://www.php.net/manual/en/function.json-decode.php">
    <link rel="shorturl" href="https://www.php.net/json-decode">
    <link rel="alternate" href="https://www.php.net/json-decode" hreflang="x-default">

    <link rel="contents" href="https://www.php.net/manual/en/index.php">
    <link rel="index" href="https://www.php.net/manual/en/ref.json.php">
    <link rel="prev" href="https://www.php.net/manual/en/ref.json.php">
    <link rel="next" href="https://www.php.net/manual/en/function.json-encode.php">

    <link rel="alternate" href="https://www.php.net/manual/en/function.json-decode.php" hreflang="en">
    <link rel="alternate" href="https://www.php.net/manual/pt_BR/function.json-decode.php" hreflang="pt_BR">
    <link rel="alternate" href="https://www.php.net/manual/zh/function.json-decode.php" hreflang="zh">
    <link rel="alternate" href="https://www.php.net/manual/fr/function.json-decode.php" hreflang="fr">
    <link rel="alternate" href="https://www.php.net/manual/de/function.json-decode.php" hreflang="de">
    <link rel="alternate" href="https://www.php.net/manual/ja/function.json-decode.php" hreflang="ja">
    <link rel="alternate" href="https://www.php.net/manual/ro/function.json-decode.php" hreflang="ro">
    <link rel="alternate" href="https://www.php.net/manual/ru/function.json-decode.php" hreflang="ru">
    <link rel="alternate" href="https://www.php.net/manual/es/function.json-decode.php" hreflang="es">
    <link rel="alternate" href="https://www.php.net/manual/tr/function.json-decode.php" hreflang="tr">

    <link rel="stylesheet" type="text/css" href="/cached.php?t=1539771603&amp;f=/fonts/Fira/fira.css" media="screen">
    <link rel="stylesheet" type="text/css" href="/cached.php?t=1539765004&amp;f=/fonts/Font-Awesome/css/fontello.css" media="screen">
    <link rel="stylesheet" type="text/css" href="/cached.php?t=1598370002&amp;f=/styles/theme-base.css" media="screen">
    <link rel="stylesheet" type="text/css" href="/cached.php?t=1540425603&amp;f=/styles/theme-medium.css" media="screen">

    <!--[if lte IE 7]>
    <link rel="stylesheet" type="text/css" href="https://www.php.net/styles/workarounds.ie7.css" media="screen">
    <![endif]-->

    <!--[if lte IE 8]>
    <script>
        window.brokenIE = true;
    </script>
    <![endif]-->

    <!--[if lte IE 9]>
    <link rel="stylesheet" type="text/css" href="https://www.php.net/styles/workarounds.ie9.css" media="screen">
    <![endif]-->

    <!--[if IE]>
    <script src="https://www.php.net/js/ext/html5.js"></script>
    <![endif]-->

    <base href="https://www.php.net/manual/en/function.json-decode.php">

</head>
<body class="docs ">

<nav id="head-nav" class="navbar navbar-fixed-top">
    <div class="navbar-inner clearfix">
        <a href="/" class="brand"><img src="/images/logos/php-logo.svg" width="48" height="24" alt="php"></a>
        <div id="mainmenu-toggle-overlay"></div>
        <input type="checkbox" id="mainmenu-toggle">
        <ul class="nav">
            <li class=""><a href="/downloads">Downloads</a></li>
            <li class="active"><a href="/docs.php">Documentation</a></li>
            <li class=""><a href="/get-involved" >Get Involved</a></li>
            <li class=""><a href="/support">Help</a></li>
        </ul>
        <form class="navbar-search" id="topsearch" action="/search.php">
            <input type="hidden" name="show" value="quickref">
            <input type="search" name="pattern" class="search-query" placeholder="Search" accesskey="s">
        </form>
    </div>
    <div id="flash-message"></div>
</nav>
<nav id="trick"><div><dl>
    <dt><a href='/manual/en/getting-started.php'>Getting Started</a></dt>
    <dd><a href='/manual/en/introduction.php'>Introduction</a></dd>
    <dd><a href='/manual/en/tutorial.php'>A simple tutorial</a></dd>
    <dt><a href='/manual/en/langref.php'>Language Reference</a></dt>
    <dd><a href='/manual/en/language.basic-syntax.php'>Basic syntax</a></dd>
    <dd><a href='/manual/en/language.types.php'>Types</a></dd>
    <dd><a href='/manual/en/language.variables.php'>Variables</a></dd>
    <dd><a href='/manual/en/language.constants.php'>Constants</a></dd>
    <dd><a href='/manual/en/language.expressions.php'>Expressions</a></dd>
    <dd><a href='/manual/en/language.operators.php'>Operators</a></dd>
    <dd><a href='/manual/en/language.control-structures.php'>Control Structures</a></dd>
    <dd><a href='/manual/en/language.functions.php'>Functions</a></dd>
    <dd><a href='/manual/en/language.oop5.php'>Classes and Objects</a></dd>
    <dd><a href='/manual/en/language.namespaces.php'>Namespaces</a></dd>
    <dd><a href='/manual/en/language.errors.php'>Errors</a></dd>
    <dd><a href='/manual/en/language.exceptions.php'>Exceptions</a></dd>
    <dd><a href='/manual/en/language.generators.php'>Generators</a></dd>
    <dd><a href='/manual/en/language.references.php'>References Explained</a></dd>
    <dd><a href='/manual/en/reserved.variables.php'>Predefined Variables</a></dd>
    <dd><a href='/manual/en/reserved.exceptions.php'>Predefined Exceptions</a></dd>
    <dd><a href='/manual/en/reserved.interfaces.php'>Predefined Interfaces and Classes</a></dd>
    <dd><a href='/manual/en/context.php'>Context options and parameters</a></dd>
    <dd><a href='/manual/en/wrappers.php'>Supported Protocols and Wrappers</a></dd>
</dl>
    <dl>
        <dt><a href='/manual/en/security.php'>Security</a></dt>
        <dd><a href='/manual/en/security.intro.php'>Introduction</a></dd>
        <dd><a href='/manual/en/security.general.php'>General considerations</a></dd>
        <dd><a href='/manual/en/security.cgi-bin.php'>Installed as CGI binary</a></dd>
        <dd><a href='/manual/en/security.apache.php'>Installed as an Apache module</a></dd>
        <dd><a href='/manual/en/security.sessions.php'>Session Security</a></dd>
        <dd><a href='/manual/en/security.filesystem.php'>Filesystem Security</a></dd>
        <dd><a href='/manual/en/security.database.php'>Database Security</a></dd>
        <dd><a href='/manual/en/security.errors.php'>Error Reporting</a></dd>
        <dd><a href='/manual/en/security.globals.php'>Using Register Globals</a></dd>
        <dd><a href='/manual/en/security.variables.php'>User Submitted Data</a></dd>
        <dd><a href='/manual/en/security.magicquotes.php'>Magic Quotes</a></dd>
        <dd><a href='/manual/en/security.hiding.php'>Hiding PHP</a></dd>
        <dd><a href='/manual/en/security.current.php'>Keeping Current</a></dd>
        <dt><a href='/manual/en/features.php'>Features</a></dt>
        <dd><a href='/manual/en/features.http-auth.php'>HTTP authentication with PHP</a></dd>
        <dd><a href='/manual/en/features.cookies.php'>Cookies</a></dd>
        <dd><a href='/manual/en/features.sessions.php'>Sessions</a></dd>
        <dd><a href='/manual/en/features.xforms.php'>Dealing with XForms</a></dd>
        <dd><a href='/manual/en/features.file-upload.php'>Handling file uploads</a></dd>
        <dd><a href='/manual/en/features.remote-files.php'>Using remote files</a></dd>
        <dd><a href='/manual/en/features.connection-handling.php'>Connection handling</a></dd>
        <dd><a href='/manual/en/features.persistent-connections.php'>Persistent Database Connections</a></dd>
        <dd><a href='/manual/en/features.commandline.php'>Command line usage</a></dd>
        <dd><a href='/manual/en/features.gc.php'>Garbage Collection</a></dd>
        <dd><a href='/manual/en/features.dtrace.php'>DTrace Dynamic Tracing</a></dd>
    </dl>
    <dl>
        <dt><a href='/manual/en/funcref.php'>Function Reference</a></dt>
        <dd><a href='/manual/en/refs.basic.php.php'>Affecting PHP's Behaviour</a></dd>
        <dd><a href='/manual/en/refs.utilspec.audio.php'>Audio Formats Manipulation</a></dd>
        <dd><a href='/manual/en/refs.remote.auth.php'>Authentication Services</a></dd>
        <dd><a href='/manual/en/refs.utilspec.cmdline.php'>Command Line Specific Extensions</a></dd>
        <dd><a href='/manual/en/refs.compression.php'>Compression and Archive Extensions</a></dd>
        <dd><a href='/manual/en/refs.crypto.php'>Cryptography Extensions</a></dd>
        <dd><a href='/manual/en/refs.database.php'>Database Extensions</a></dd>
        <dd><a href='/manual/en/refs.calendar.php'>Date and Time Related Extensions</a></dd>
        <dd><a href='/manual/en/refs.fileprocess.file.php'>File System Related Extensions</a></dd>
        <dd><a href='/manual/en/refs.international.php'>Human Language and Character Encoding Support</a></dd>
        <dd><a href='/manual/en/refs.utilspec.image.php'>Image Processing and Generation</a></dd>
        <dd><a href='/manual/en/refs.remote.mail.php'>Mail Related Extensions</a></dd>
        <dd><a href='/manual/en/refs.math.php'>Mathematical Extensions</a></dd>
        <dd><a href='/manual/en/refs.utilspec.nontext.php'>Non-Text MIME Output</a></dd>
        <dd><a href='/manual/en/refs.fileprocess.process.php'>Process Control Extensions</a></dd>
        <dd><a href='/manual/en/refs.basic.other.php'>Other Basic Extensions</a></dd>
        <dd><a href='/manual/en/refs.remote.other.php'>Other Services</a></dd>
        <dd><a href='/manual/en/refs.search.php'>Search Engine Extensions</a></dd>
        <dd><a href='/manual/en/refs.utilspec.server.php'>Server Specific Extensions</a></dd>
        <dd><a href='/manual/en/refs.basic.session.php'>Session Extensions</a></dd>
        <dd><a href='/manual/en/refs.basic.text.php'>Text Processing</a></dd>
        <dd><a href='/manual/en/refs.basic.vartype.php'>Variable and Type Related Extensions</a></dd>
        <dd><a href='/manual/en/refs.webservice.php'>Web Services</a></dd>
        <dd><a href='/manual/en/refs.utilspec.windows.php'>Windows Only Extensions</a></dd>
        <dd><a href='/manual/en/refs.xml.php'>XML Manipulation</a></dd>
        <dd><a href='/manual/en/refs.ui.php'>GUI Extensions</a></dd>
    </dl>
    <dl>
        <dt>Keyboard Shortcuts</dt><dt>?</dt>
        <dd>This help</dd>
        <dt>j</dt>
        <dd>Next menu item</dd>
        <dt>k</dt>
        <dd>Previous menu item</dd>
        <dt>g p</dt>
        <dd>Previous man page</dd>
        <dt>g n</dt>
        <dd>Next man page</dd>
        <dt>G</dt>
        <dd>Scroll to bottom</dd>
        <dt>g g</dt>
        <dd>Scroll to top</dd>
        <dt>g h</dt>
        <dd>Goto homepage</dd>
        <dt>g s</dt>
        <dd>Goto search<br>(current page)</dd>
        <dt>/</dt>
        <dd>Focus search box</dd>
    </dl></div></nav>
<div id="goto">
    <div class="search">
        <div class="text"></div>
        <div class="results"><ul></ul></div>
    </div>
</div>

<div id="breadcrumbs" class="clearfix">
    <div id="breadcrumbs-inner">
        <div class="next">
            <a href="function.json-encode.php">
                json_encode &raquo;
            </a>
        </div>
        <div class="prev">
            <a href="ref.json.php">
                &laquo; JSON Functions        </a>
        </div>
        <ul>
            <li><a href='index.php'>PHP Manual</a></li>      <li><a href='funcref.php'>Function Reference</a></li>      <li><a href='refs.basic.other.php'>Other Basic Extensions</a></li>      <li><a href='book.json.php'>JSON</a></li>      <li><a href='ref.json.php'>JSON Functions</a></li>      </ul>
    </div>
</div>




<div id="layout" class="clearfix">
    <section id="layout-content">
        <div class="page-tools">
            <div class="change-language">
                <form action="/manual/change.php" method="get" id="changelang" name="changelang">
                    <fieldset>
                        <label for="changelang-langs">Change language:</label>
                        <select onchange="document.changelang.submit()" name="page" id="changelang-langs">
                            <option value='en/function.json-decode.php' selected="selected">English</option>
                            <option value='pt_BR/function.json-decode.php'>Brazilian Portuguese</option>
                            <option value='zh/function.json-decode.php'>Chinese (Simplified)</option>
                            <option value='fr/function.json-decode.php'>French</option>
                            <option value='de/function.json-decode.php'>German</option>
                            <option value='ja/function.json-decode.php'>Japanese</option>
                            <option value='ro/function.json-decode.php'>Romanian</option>
                            <option value='ru/function.json-decode.php'>Russian</option>
                            <option value='es/function.json-decode.php'>Spanish</option>
                            <option value='tr/function.json-decode.php'>Turkish</option>
                            <option value="help-translate.php">Other</option>
                        </select>
                    </fieldset>
                </form>
            </div>
            <div class="edit-bug">
                <a href="https://edit.php.net/?project=PHP&amp;perm=en/function.json-decode.php">Edit</a>
                <a href="https://bugs.php.net/report.php?bug_type=Documentation+problem&amp;manpage=function.json-decode">Report a Bug</a>
            </div>
        </div><div id="function.json-decode" class="refentry">
        <div class="refnamediv">
            <h1 class="refname">json_decode</h1>
            <p class="verinfo">(PHP 5 &gt;= 5.2.0, PHP 7, PECL json &gt;= 1.2.0)</p><p class="refpurpose"><span class="refname">json_decode</span> &mdash; <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="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>NULL</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>
                ]]] ) : <span class="type"><a href="language.pseudo-types.php#language.types.mixed" class="type mixed">mixed</a></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">string</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">&raquo;&nbsp;RFC 7159</a>.
                </p>
                </p></blockquote>

            </dd>



            <dt>
                <code class="parameter">assoc</code></dt>

            <dd>

                <p class="para">
                    When <strong><code>TRUE</code></strong>, JSON objects will be returned as
                    associative <span class="type">array</span>s; when <strong><code>FALSE</code></strong>, JSON objects will be returned as <span class="type">object</span>s.
                    When <strong><code>NULL</code></strong>, JSON objects will be returned as associative <span class="type">array</span>s or
                    <span class="type">object</span>s depending on whether <strong><code>JSON_OBJECT_AS_ARRAY</code></strong>
                    is set in the <code class="parameter">options</code>.
                </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>,
                    <strong><code>JSON_INVALID_UTF8_IGNORE</code></strong>,
                    <strong><code>JSON_INVALID_UTF8_SUBSTITUTE</code></strong>,
                    <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.php" 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 <code class="literal">true</code>, <code class="literal">false</code> and
                <code class="literal">null</code> 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 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.3.0</td>
                    <td>
                        <strong><code>JSON_THROW_ON_ERROR</code></strong>
                        <code class="parameter">options</code> was added.
                    </td>
                </tr>

                <tr>
                    <td>7.2.0</td>
                    <td>
                        <code class="parameter">assoc</code> is nullable now.
                    </td>
                </tr>

                <tr>
                    <td>7.2.0</td>
                    <td>
                        <strong><code>JSON_INVALID_UTF8_IGNORE</code></strong>, and
                        <strong><code>JSON_INVALID_UTF8_SUBSTITUTE</code></strong>
                        <code class="parameter">options</code> were added.
                    </td>
                </tr>

                <tr>
                    <td>7.1.0</td>
                    <td>
                        An empty JSON key (&quot;&quot;) can be encoded to the empty object property
                        instead of using a key with value <code class="literal">_empty_</code>.
                    </td>
                </tr>

                </tbody>

            </table>

            </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-3942">
            <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">&lt;?php<br />$json&nbsp;</span><span style="color: #007700">=&nbsp;</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">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</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) {
    [&quot;a&quot;] =&gt; int(1)
    [&quot;b&quot;] =&gt; int(2)
    [&quot;c&quot;] =&gt; int(3)
    [&quot;d&quot;] =&gt; int(4)
    [&quot;e&quot;] =&gt; int(5)
}

array(5) {
    [&quot;a&quot;] =&gt; int(1)
    [&quot;b&quot;] =&gt; int(2)
    [&quot;c&quot;] =&gt; int(3)
    [&quot;d&quot;] =&gt; int(4)
    [&quot;e&quot;] =&gt; int(5)
}
</pre></div>
            </div>
        </div>
        <div class="example" id="example-3943">
            <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&#039;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">&lt;?php<br /><br />$json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'{"foo-bar":&nbsp;12345}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">=&nbsp;</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&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;{</span><span style="color: #DD0000">'foo-bar'</span><span style="color: #007700">};&nbsp;</span><span style="color: #FF8000">//&nbsp;12345<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
                </code></div>
            </div>

        </div>
        <div class="example" id="example-3944">
            <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">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;the&nbsp;following&nbsp;strings&nbsp;are&nbsp;valid&nbsp;JavaScript&nbsp;but&nbsp;not&nbsp;valid&nbsp;JSON<br /><br />//&nbsp;the&nbsp;name&nbsp;and&nbsp;value&nbsp;must&nbsp;be&nbsp;enclosed&nbsp;in&nbsp;double&nbsp;quotes<br />//&nbsp;single&nbsp;quotes&nbsp;are&nbsp;not&nbsp;valid&nbsp;<br /></span><span style="color: #0000BB">$bad_json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"{&nbsp;'bar':&nbsp;'baz'&nbsp;}"</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">);&nbsp;</span><span style="color: #FF8000">//&nbsp;null<br /><br />//&nbsp;the&nbsp;name&nbsp;must&nbsp;be&nbsp;enclosed&nbsp;in&nbsp;double&nbsp;quotes<br /></span><span style="color: #0000BB">$bad_json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'{&nbsp;bar:&nbsp;"baz"&nbsp;}'</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">);&nbsp;</span><span style="color: #FF8000">//&nbsp;null<br /><br />//&nbsp;trailing&nbsp;commas&nbsp;are&nbsp;not&nbsp;allowed<br /></span><span style="color: #0000BB">$bad_json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'{&nbsp;bar:&nbsp;"baz",&nbsp;}'</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">);&nbsp;</span><span style="color: #FF8000">//&nbsp;null<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
                </code></div>
            </div>

        </div>
        <div class="example" id="example-3945">
            <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">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Encode&nbsp;the&nbsp;data.<br /></span><span style="color: #0000BB">$json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'English'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'One'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'January'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'French'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'Une'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'Janvier'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />&nbsp;&nbsp;&nbsp;&nbsp;)<br />);<br /><br /></span><span style="color: #FF8000">//&nbsp;Define&nbsp;the&nbsp;errors.<br /></span><span style="color: #0000BB">$constants&nbsp;</span><span style="color: #007700">=&nbsp;</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&nbsp;</span><span style="color: #007700">=&nbsp;array();<br />foreach&nbsp;(</span><span style="color: #0000BB">$constants</span><span style="color: #007700">[</span><span style="color: #DD0000">"json"</span><span style="color: #007700">]&nbsp;as&nbsp;</span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">strncmp</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"JSON_ERROR_"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">11</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$json_errors</span><span style="color: #007700">[</span><span style="color: #0000BB">$value</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Show&nbsp;the&nbsp;errors&nbsp;for&nbsp;different&nbsp;depths.<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$depth</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</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">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$depth</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Last&nbsp;error:&nbsp;'</span><span style="color: #007700">,&nbsp;</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">()],&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</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]=&gt;
  array(2) {
    [&quot;English&quot;]=&gt;
    array(2) {
      [0]=&gt;
      string(3) &quot;One&quot;
      [1]=&gt;
      string(7) &quot;January&quot;
    }
    [&quot;French&quot;]=&gt;
    array(2) {
      [0]=&gt;
      string(3) &quot;Une&quot;
      [1]=&gt;
      string(7) &quot;Janvier&quot;
    }
  }
}
Last error: JSON_ERROR_NONE

NULL
Last error: JSON_ERROR_DEPTH
</pre></div>
            </div>
        </div>
        <div class="example" id="example-3946">
            <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">&lt;?php<br />$json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'{"number":&nbsp;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">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">512</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">JSON_BIGINT_AS_STRING</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</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) {
  [&quot;number&quot;]=&gt;
  float(1.2345678901235E+19)
}
object(stdClass)#1 (1) {
  [&quot;number&quot;]=&gt;
  string(20) &quot;12345678901234567890&quot;
}
</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.php" 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 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.php" 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.php" class="function" rel="rdfs-seeAlso">json_last_error()</a> - Returns the last error occurred</span></li>
            </ul>
            </p>
        </div>

</div>
<section id="usernotes">
    <div class="head">
        <span class="action"><a href="/manual/add-note.php?sect=function.json-decode&amp;redirect=https://www.php.net/manual/en/function.json-decode.php"><img src='/images/notes-add@2x.png' alt='add a note' width='12' height='12'> <small>add a note</small></a></span>
        <h3 class="title">User Contributed Notes <span class="count">1 note</span></h3>
    </div><div id="allnotes">
    <div class="note" id="125505">  <div class="votes">
        <div id="Vu125505">
            <a href="/manual/vote-note.php?id=125505&amp;page=function.json-decode&amp;vote=up" title="Vote up!" class="usernotes-voteu">up</a>
        </div>
        <div id="Vd125505">
            <a href="/manual/vote-note.php?id=125505&amp;page=function.json-decode&amp;vote=down" title="Vote down!" class="usernotes-voted">down</a>
        </div>
        <div class="tally" id="V125505" title="100% like this...">
            1
        </div>
    </div>
        <a href="#125505" class="name">
            <strong class="user"><em>cubefox at web dot NOSPAMPLEASE dot de</em></strong></a><a class="genanchor" href="#125505"> &para;</a><div class="date" title="2020-11-11 01:18"><strong>3 days ago</strong></div>
        <div class="text" id="Hcom125505">
            <div class="phpcode"><code><span class="html">
Warning: As the section "return values" mentions, the return value NULL is ambiguos. To repeat, it can mean three things:<br /><br />* The input string had the value "null"<br />* There was an error while parsing the input data<br />* The encoded data was deeper than the recursion limit<br /><br />To distinguish these cases, json_last_error() can be used.</span>
            </code></div>
        </div>
    </div></div>

    <div class="foot"><a href="/manual/add-note.php?sect=function.json-decode&amp;redirect=https://www.php.net/manual/en/function.json-decode.php"><img src='/images/notes-add@2x.png' alt='add a note' width='12' height='12'> <small>add a note</small></a></div>
</section>    </section><!-- layout-content -->
<aside class='layout-menu'>

    <ul class='parent-menu-list'>
        <li>
            <a href="ref.json.php">JSON Functions</a>

            <ul class='child-menu-list'>

                <li class="current">
                    <a href="function.json-decode.php" title="json_&#8203;decode">json_&#8203;decode</a>
                </li>
                <li class="">
                    <a href="function.json-encode.php" title="json_&#8203;encode">json_&#8203;encode</a>
                </li>
                <li class="">
                    <a href="function.json-last-error-msg.php" title="json_&#8203;last_&#8203;error_&#8203;msg">json_&#8203;last_&#8203;error_&#8203;msg</a>
                </li>
                <li class="">
                    <a href="function.json-last-error.php" title="json_&#8203;last_&#8203;error">json_&#8203;last_&#8203;error</a>
                </li>

            </ul>

        </li>

    </ul>
</aside>


</div><!-- layout -->

<footer>
    <div class="container footer-content">
        <div class="row-fluid">
            <ul class="footmenu">
                <li><a href="/copyright.php">Copyright &copy; 2001-2020 The PHP Group</a></li>
                <li><a href="/my.php">My PHP.net</a></li>
                <li><a href="/contact.php">Contact</a></li>
                <li><a href="/sites.php">Other PHP.net sites</a></li>
                <li><a href="/privacy.php">Privacy policy</a></li>
            </ul>
        </div>
    </div>
</footer>


<!-- External and third party libraries. -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="/cached.php?t=1421837618&amp;f=/js/ext/modernizr.js"></script>
<script src="/cached.php?t=1421837618&amp;f=/js/ext/hogan-2.0.0.min.js"></script>
<script src="/cached.php?t=1421837618&amp;f=/js/ext/typeahead.min.js"></script>
<script src="/cached.php?t=1421837618&amp;f=/js/ext/mousetrap.min.js"></script>
<script src="/cached.php?t=1421837618&amp;f=/js/search.js"></script>
<script src="/cached.php?t=1604317202&amp;f=/js/common.js"></script>

<a id="toTop" href="javascript:;"><span id="toTopHover"></span><img width="40" height="40" alt="To Top" src="/images/to-top@2x.png"></a>

</body>
</html>
