title: Cannot extract field from a non-object, Postgres json array
body: <p>I have a table called <code>Stores</code> which has a key called <code>store_id</code> and a table called <code>Sales</code> which contains a <code>store_id</code> reference and a json field called <code>sales_json</code>.</p>
  
  <p><code>sales_json</code> looks something like this:</p>
  
  <pre><code>[{'start_date': '2-20-15', 'end_date': '2-21-15', 'start_time': '11:00 AM', 
    'end_time': '11:00 PM', 'discount_percentage': 20}, etc] 
  </code></pre>
  
  <p>I have a plpgsql function in which I'm trying to determine if a store is currently having a sale and store a boolean called <code>having_sale</code> in the result table depending on whether this is true or not.</p>
  
  <p>My SELECT statement looks like this:</p>
  
  <pre><code>SELECT Stores.store_id,
         CASE WHEN (SELECT COUNT(*)
                    FROM (SELECT *
                          FROM json_array_elements(sales_json) AS sale
                          WHERE (now()::date BETWEEN (sale-&gt;&gt;'start_date')::date AND
                                                     (sale-&gt;&gt;'end_date')::date) AND
                                (now()::time BETWEEN (sale-&gt;&gt;'start_time')::time AND
                                                     (sale-&gt;&gt;'end_time')::time)
                         ) AS current_sales) &gt; 0
                    THEN TRUE
              ELSE FALSE
         END) AS having_sale
  FROM Stores INNER JOIN Sales
  ON Stores.store_id = Sales.store_id;
  </code></pre>
  
  <p>The code looks correct to me but I get the following error:</p>
  
  <pre><code>psycopg2.DataError: cannot extract field from a non-object
  </code></pre>
  
  <p>What am I doing wrong and how do I fix it?</p>