title: Query for array elements inside JSON type
body: <p>I'm trying to test out the <code>json</code> type in PostgreSQL 9.3.<br>
  I have a <code>json</code> column called <code>data</code> in a table called <code>reports</code>. The JSON looks something like this:</p>
  
  <pre><code>{
    "objects": [
      {"src":"foo.png"},
      {"src":"bar.png"}
    ],
    "background":"background.png"
  }
  </code></pre>
  
  <p>I would like to query the table for all reports that match the 'src' value in the 'objects' array. For example, is it possible to query the DB for all reports that match <code>'src' = 'foo.png'</code>? I successfully wrote a query that can match the <code>"background"</code>:</p>
  
  <pre><code>SELECT data AS data FROM reports where data-&gt;&gt;'background' = 'background.png'
  </code></pre>
  
  <p>But since <code>"objects"</code> has an array of values, I can't seem to write something that works. Is it possible to query the DB for all reports that match <code>'src' = 'foo.png'</code>? I've looked through these sources but still can't get it:</p>
  
  <ul>
  <li><a href="http://www.postgresql.org/docs/9.3/static/functions-json.html" rel="noreferrer">http://www.postgresql.org/docs/9.3/static/functions-json.html</a></li>
  <li><a href="https://stackoverflow.com/questions/10560394/how-do-i-query-using-fields-inside-the-new-postgresql-json-datatype">How do I query using fields inside the new PostgreSQL JSON datatype?</a></li>
  <li><a href="http://michael.otacoo.com/postgresql-2/postgres-9-3-feature-highlight-json-operators/" rel="noreferrer">http://michael.otacoo.com/postgresql-2/postgres-9-3-feature-highlight-json-operators/</a></li>
  </ul>
  
  <p>I've also tried things like this but to no avail:</p>
  
  <pre><code>SELECT json_array_elements(data-&gt;'objects') AS data from reports
  WHERE  data-&gt;&gt;'src' = 'foo.png';
  </code></pre>
  
  <p>I'm not an SQL expert, so I don't know what I am doing wrong.</p>