sqlalchemy JSON query rows without a specific key (Key existence)
<p>When using sqlalchemy with postgresql, I have the following table and data:</p>

<pre><code> id |   data   
----+----------
  1 | {}
  2 | {"a": 1}
(2 rows)
</code></pre>

<p>How do I find row(s) that does not have a key. e.g. "a" or data["a"]?</p>

<pre><code>Give me all objects that does not have the key a.
 id |   data   
----+----------
  1 | {}
(1 row)
</code></pre>

<hr>

<pre><code>self.session.query(Json_test).filter(???)
</code></pre>

<p>If the column type is <code>jsonb</code> you can use <a href="http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#sqlalchemy.dialects.postgresql.JSONB" rel="nofollow"><code>has_key</code></a>:</p>

<pre><code>session.query(Json_test).filter(sqlalchemy.not_(Json_test.data.has_key('a')))
</code></pre>

<p>For both <code>json</code> and <code>jsonb</code> types this should work:</p>

<pre><code>session.query(Json_test).filter(Json_test.data.op('-&gt;')('a')==None)
</code></pre>

python postgresql sqlalchemy