title: Deconstructing a JSON object
body: <p>A JSON encoded array is passed from PHP to an HTML document.  It is not at all clear how to deconstruct that array into javascript-usable pieces.  For example, consider the following HTML:</p>
  <p><code>&lt;div id=&quot;options&quot;&gt;{&quot;foo&quot;:[{&quot;id&quot;:1},{&quot;id&quot;:3}], &quot;bar&quot;:[{&quot;id&quot;:2},{&quot;id&quot;:4}]}&lt;/div&gt;</code></p>
  <p>The only <em>a priori</em> known element of this array is that the key <code>id</code> exists. The indices, I know, can be found with</p>
  <pre><code>var data = JSON.parse($(&quot;#options&quot;).text());
  $.each(data, function(index) {
    // index will be foo &amp; bar
  });
  </code></pre>
  <p>The use case is to use the index and id to add an attribute to elements in a document. I have not yet stumbled upon the technique to return the ids associated with each index. How best can that be done?</p>
  <h1>Edit - a clarification of the use case - the long story</h1>
  <p>I want to re-enable some options on a form based on properties of an entity (in a Symfony application).  Disabled options cannot be modified, but are also not not persisted - their values are set to null.  I've built a service to determine the option elements that are disabled and send those elements to the form document as a JSON object.  I'm assuming for now that the specific options are not known until the form is created.  In the example above, <code>foo</code> &amp; <code>bar</code> represent possible options, and the ids correspond to the option.  For example, a <code>Household</code> entity might have <code>Reason</code> options selected but disabled of &quot;Low wages&quot; (id = 3).  This would show up in as <code>...id=&quot;options&quot;&gt;{&quot;reasons&quot;:[{&quot;id&quot;:3}]}&lt;...</code>.  I would the use this information to remove the <code>disabled=&quot;disabled&quot;</code> attribute from the set of checkboxes for the Reason, id=3 (i.e., <code>id=&quot;household_reasons_3&quot;</code>) field.  I hope this makes sense.</p>
  <h2>Edit #2, by request - the PHP code creating the object.</h2>
  <p>The result of <code>getMetatData()</code> appears in the document at <code>#options</code>.  From the above edit, the Household entity is <code>$object</code>.</p>
  <pre><code>public function getMetaData($object) {
      $data = array();
      $className = get_class($object);
      $metaData = $this-&gt;em-&gt;getClassMetadata($className);
      foreach ($metaData-&gt;associationMappings as $field =&gt; $mapping) {
          if (8 === $mapping['type']) {
              $data[$field] = $this-&gt;extractOptions($object, $field);
          }
      }
  
      return json_encode($data);
  }
  
  private function extractOptions($object, $field) {
      $data = [];
      $method = 'get' . ucfirst($field);
      $itemName = substr($field, 0, -1);
      $getter = 'get' . ucfirst($itemName);
      $entity = $object-&gt;$method();
      foreach ($entity as $item) {
          if (method_exists($item, 'getEnabled') &amp;&amp; false === $item-&gt;getEnabled()) {
              $data[] = ['id' =&gt; $item-&gt;getId()];
          }
      }
  
      return $data;
  }
  </code></pre>