<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.json.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'de',
  ),
  'this' => 
  array (
    0 => 'function.json-validate.php',
    1 => 'json_validate',
    2 => 'Checks if a string contains valid JSON',
  ),
  'up' => 
  array (
    0 => 'ref.json.php',
    1 => 'JSON-Funktionen',
  ),
  'prev' => 
  array (
    0 => 'function.json-last-error-msg.php',
    1 => 'json_last_error_msg',
  ),
  'next' => 
  array (
    0 => 'book.simdjson.php',
    1 => 'Simdjson',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/json/functions/json-validate.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.json-validate" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">json_validate</h1>
  <p class="verinfo">(PHP 8 &gt;= 8.3.0)</p><p class="refpurpose"><span class="refname">json_validate</span> &mdash; <span class="dc-title">Checks if a string contains valid JSON</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.json-validate-description">
  <h3 class="title">Beschreibung</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>json_validate</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$json</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$depth</code><span class="initializer"> = 512</span></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">
   Returns whether the given <a href="language.types.string.php" class="link">String</a> is syntactically valid JSON.
   If <span class="function"><strong>json_validate()</strong></span> returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, <span class="function"><a href="function.json-decode.php" class="function">json_decode()</a></span>
   will successfully decode the given string when using the same
   <code class="parameter">depth</code> and <code class="parameter">flags</code>.
  </p>
  <p class="para">
   If <span class="function"><strong>json_validate()</strong></span> returns <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, the cause
   can be retrieved using <span class="function"><a href="function.json-last-error.php" class="function">json_last_error()</a></span> and
   <span class="function"><a href="function.json-last-error-msg.php" class="function">json_last_error_msg()</a></span>.
  </p>
  <p class="para">
   <span class="function"><strong>json_validate()</strong></span> uses less memory than
   <span class="function"><a href="function.json-decode.php" class="function">json_decode()</a></span> if the decoded JSON payload is
   not used, because it does not need to build the array or
   object structure containing the payload.
  </p>
  <div class="caution"><strong class="caution">Achtung</strong>
   <p class="para">
    Calling <span class="function"><strong>json_validate()</strong></span> immediately before
    <span class="function"><a href="function.json-decode.php" class="function">json_decode()</a></span> will unnecessarily parse the string
    twice, as <span class="function"><a href="function.json-decode.php" class="function">json_decode()</a></span> implicitly performs
    validation during decoding.
   </p>
   <p class="para">
    <span class="function"><strong>json_validate()</strong></span> should therefore only be used
    if the decode JSON payload is not immediately used and knowing whether
    the string contains valid JSON is needed.
   </p>
  </div>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.json-validate-parameters">
  <h3 class="title">Parameter-Liste</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">json</code></dt>
     <dd>
      <p class="para">
       The string to validate.
      </p>
      <p class="para">
       This function only works with UTF-8 encoded strings.
      </p>
      
 <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
  <p class="para">
   PHP implementiert eine Obermenge von JSON wie im Original
   <a href="https://datatracker.ietf.org/doc/html/rfc7159" class="link external">&raquo;&nbsp;RFC 7159</a> beschrieben.
  </p>
 </p></blockquote>

     </dd>
    
    
     <dt><code class="parameter">depth</code></dt>
     <dd>
      <p class="para">
       Maximum nesting depth of the structure being decoded.
       The value must be greater than <code class="literal">0</code>,
       and less than or equal to <code class="literal">2147483647</code>.
      </p>
     </dd>
    
    
     <dt><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       Currently only
       <strong><code><a href="json.constants.php#constant.json-invalid-utf8-ignore">JSON_INVALID_UTF8_IGNORE</a></code></strong>
       is accepted.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.json-validate-returnvalues">
  <h3 class="title">Rückgabewerte</h3>
  <p class="para">
   Returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> if the given string is syntactically valid JSON, otherwise
   returns <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.json-validate-errors">
  <h3 class="title">Fehler/Exceptions</h3>
  <p class="para">
   If <code class="parameter">depth</code> is outside the allowed range,
   a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> is thrown.
  </p>
  <p class="para">
   If <code class="parameter">flags</code> is not a valid flag,
   a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> is thrown.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.json-validate-examples">
  <h3 class="title">Beispiele</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Beispiel #1 <span class="function"><strong>json_validate()</strong></span> examples</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_validate</span><span style="color: #007700">(</span><span style="color: #DD0000">'{ "test": { "foo": "bar" } }'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_validate</span><span style="color: #007700">(</span><span style="color: #DD0000">'{ "": "": "" } }'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">bool(true)
bool(false)</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.json-validate-seealso">
  <h3 class="title">Siehe auch</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.json-decode.php" class="function" rel="rdfs-seeAlso">json_decode()</a> - Dekodiert eine JSON-Zeichenkette</span></li>
    <li><span class="function"><a href="function.json-last-error.php" class="function" rel="rdfs-seeAlso">json_last_error()</a> - Gibt den letzten aufgetretenen Fehler zur&uuml;ck</span></li>
    <li><span class="function"><a href="function.json-last-error-msg.php" class="function" rel="rdfs-seeAlso">json_last_error_msg()</a> - Liefert die Fehlermeldung des letzten Aufrufs von json_validate(),
   json_encode() oder json_decode()</span></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>