title: Decoding JSON in Twig
body: <p>Is is possible to decode JSON in twig? Googling doesn't seem to yield anything about this. Does decoding JSON in Twig not make sense?</p>
  
  <hr>
  
  <p>I'm trying to access 2 entity properties on an Symfony2's entity field type (<a href="http://symfony.com/doc/2.0/reference/forms/types/entity.html" rel="noreferrer">Entity Field Type</a>).</p>
  
  <p>After coming across 2 previous SO questions ( <a href="https://stackoverflow.com/q/9915968/1349295">Symfony2 entity field type alternatives to &quot;property&quot; or &quot;__toString()&quot;?</a> and <a href="https://stackoverflow.com/q/13070595/1349295">Symfony 2 Create a entity form field with 2 properties</a> ) which suggested adding an extra method to an entity to retrieve a customized string rather than an entity attribute, I thought of (and did) returning a JSON string representing an object instance.</p>
  
  <p>Somewhere in the entity class:</p>
  
  <pre><code>/**
   * Return a JSON string representing this class.
   */
  public function getJson()
  {
     return json_encode(get_object_vars($this));
  }
  </code></pre>
  
  <p>And in the form (something like):</p>
  
  <pre><code>$builder-&gt;add('categories', 'entity', array (
  ...
  'property' =&gt; 'json',
  ...
  ));
  </code></pre>
  
  <p>Afterwards, I was hoping to <code>json_decode</code> it in Twig...</p>
  
  <pre><code>{% for category in form.categories %}
      {# json_decode() part is imaginary #}
      {% set obj = category.vars.label|json_decode() %}
  {% endfor %}
  </code></pre>