Answers.Body: <p>I don’t think you can catch exceptions in Twig (at least without an <a href="https://github.com/gglnx/twig-try-catch" rel="nofollow noreferrer">extension</a>).</p>
  <p>Craft has a handy <a href="https://github.com/craftcms/cms/blob/5.x/src/helpers/Json.php" rel="nofollow noreferrer"><code>Json</code></a> helper class that contains a <a href="https://github.com/craftcms/cms/blob/93d2df90b3f1746bb9e1679e36fe7652cc453bd1/src/helpers/Json.php#L49" rel="nofollow noreferrer"><code>decodeIfJson()</code></a> method that returns a decoded string, if the provided value is valid JSON, otherwise the value itself.</p>
  <p>You can create an instance of this class using the <a href="https://craftcms.com/docs/5.x/reference/twig/functions.html#create" rel="nofollow noreferrer"><code>create</code></a> function, call <code>decodeIfJson(filters)</code> on it, and check the result against the value of <code>filters</code>. If the result is the same as the value, it must be invalid JSON (unless the value is an integer, in which case they would be the same).</p>
  <pre><code>{% set jsonHelper = create('\\craft\\helpers\\Json') %}
  {% set decoded = jsonHelper.decodeIfJson(filters) %}
  {{ decoded == filters ? 'Invalid' : 'Valid' }} JSON
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-09-02T14:18:22.587Z
Answers.Text: That’s very clever. Thanks so much Ben!
  
  And now I know about the [create function](https://craftcms.com/docs/4.x/dev/functions.html#create) too :)
Answers.UserDisplayName: Jonathan Schofield
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-08-30T18:07:19.130Z
Answers.LastActivityDate: 2024-08-31T07:42:05.867Z
Answers.LastEditDate: 2024-08-31T07:42:05.867Z
Answers.UserDisplayName: Ben Croker
Body: <p>I have a private template in which a JSON string can be passed in but when the string is <em>not</em> JSON compatible I want to provide something helpful, not just have the template fail.</p>
  <p>For example, I'm dynamically building an entries query which <em>could</em> have field scopes or other entry query parameters appended, so I want an optional <code>filters</code> URL parameter to be allowed to pass a JSON string.</p>
  <ul>
  <li>Thus a parameter of <code>filters={&quot;level&quot;:3}</code> passes valid JSON but <code>filters={level:3}</code> does not.</li>
  <li>Let's say the parameter is captured in a variable called <code>filters</code>…</li>
  <li>So in the first case <code>filters|json_decode</code> works but in the second it generates an error.</li>
  </ul>
  <p>I'd like to be able to do something like:</p>
  <pre><code>{% set valid = filters|json_decode is iterable %}
  </code></pre>
  <p>…and then provide error messaging on <code>false</code>.</p>
  <p>But I can't. I just get a yii syntax error template failure when the string isn't valid.</p>
  <p>Is there a solution?</p>
  
Tags: <craft4><filter><error-handling>
Title: How to capture an error on string|json_decode?
ThreadText: # How to capture an error on string|json_decode?
  
  (Asked by: username_0 on 2024-08-30)
  
  I have a private template in which a JSON string can be passed in but when the string is *not* JSON compatible I want to provide something helpful, not just have the template fail.
  
  For example, I'm dynamically building an entries query which *could* have field scopes or other entry query parameters appended, so I want an optional `filters` URL parameter to be allowed to pass a JSON string.
  
  * Thus a parameter of `filters={"level":3}` passes valid JSON but `filters={level:3}` does not.
  * Let's say the parameter is captured in a variable called `filters`…
  * So in the first case `filters|json_decode` works but in the second it generates an error.
  
  I'd like to be able to do something like:
  
  ```
  {% set valid = filters|json_decode is iterable %}
  
  ```
  
  …and then provide error messaging on `false`.
  
  But I can't. I just get a yii syntax error template failure when the string isn't valid.
  
  Is there a solution?
  
  **Tags:** craft4, filter, error-handling
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-08-30. Score: 2
  
  I don’t think you can catch exceptions in Twig (at least without an [extension](https://github.com/gglnx/twig-try-catch)).
  
  Craft has a handy [`Json`](https://github.com/craftcms/cms/blob/5.x/src/helpers/Json.php) helper class that contains a [`decodeIfJson()`](https://github.com/craftcms/cms/blob/93d2df90b3f1746bb9e1679e36fe7652cc453bd1/src/helpers/Json.php#L49) method that returns a decoded string, if the provided value is valid JSON, otherwise the value itself.
  
  You can create an instance of this class using the [`create`](https://craftcms.com/docs/5.x/reference/twig/functions.html#create) function, call `decodeIfJson(filters)` on it, and check the result against the value of `filters`. If the result is the same as the value, it must be invalid JSON (unless the value is an integer, in which case they would be the same).
  
  ```
  {% set jsonHelper = create('\\craft\\helpers\\Json') %}
  {% set decoded = jsonHelper.decodeIfJson(filters) %}
  {{ decoded == filters ? 'Invalid' : 'Valid' }} JSON
  
  ```
  
  > **Comments:**
  > **username_0**: That’s very clever. Thanks so much Ben!  And now I know about the [create function](https://craftcms.com/docs/4.x/dev/functions.html#create) too :)  
  
  ---
  
  