title: Postgresql query json element inside json array
body: <p>I have a table like:</p>
  
  <pre><code>CREATE TABLE stats
  (
    item character varying,
    data jsonb
  );
  
  it contains values like 
  ITEM        DATA
  test1       [{"id":"1", "country":"UK"},{"id":"2", "country":"INDIA"}] 
  test2       [{"id":"1", "country":"US"},{"id":"4", "country":"CHINA"},{"id":"5", "country":"CHINA"}] 
  </code></pre>
  
  <p>I need to get number of distinct json objects where country is 'CHINA' and item is '%test%';
  In this case the output should be 2, ie, [{"id":"4", "country":"CHINA"},{"id":"5", "country":"CHINA"}] </p>
  
  <p>I am using the following query </p>
  
  <pre><code>SELECT  * FROM   stats t
  WHERE ( data @&gt; '[{"country":"CHINA"}]')
  and t.item ilike '%test%';
  
  output : [{"id":"1", "country":"US"},{"id":"4", "country":"CHINA"},{"id":"5", "country":"CHINA"}]
  </code></pre>
  
  <p>What should i do so that i get the array of objects which has 'CHINA' as countries?</p>