SQLAlchemy filter according to nested keys in JSONB
I have a JSONB field that sometimes has nested keys. Example: {"nested_field": {"another URL": "foo", "a simple text": "text"}, "first_metadata": "plain string", "another_metadata": "foobar"} If I do .filter(TestMetadata.metadata_item.has_key(nested_field)) I get this record. How can I search for existence of the nested key? ( "a simple text" ) van With SQLAlchemy the following should work for your test string: class TestMetadata(Base): id = Column(Integer, primary_key=True) name = Column(String) metadata_item = Column(JSONB) as per SQLAlchemy documentation of JSONB (search for Path index