SqlAlchemy with Postgres: How can I make a query that checks if a string is in a list inside a json column

13 views
Skip to first unread message

chat...@gmail.com

unread,
Oct 13, 2021, 9:50:16 AMOct 13
to sqlalchemy

Imagine a Postgres JSON column with values like below:

"["user1", "user2"]"

Is there any way to query a postgres JSON (not JSONB) column with SqlAlchemy,like above that checks if the value "user1" is contained in this column?

Sergey V.

unread,
Oct 13, 2021, 10:49:28 PMOct 13
to sqlalchemy
Use .any():

    session.query(Gizmo).filter(Gizmo.users.any('user1'))

Jonathan Vanasco

unread,
Oct 18, 2021, 5:35:48 PMOct 18
to sqlalchemy
I'm not sure, but AFAIK, this type of search isn't *easily* doable in PostgreSQL. The json and jsonb operators and functions are really targeting "object literals" style data, not lists. 

In the past, I think one could search against the column like text and match/regex out a list value like `"user1"` - but that didn't work right.

This type of search is possible with advanced PostgreSQL queries, by using the functions like json_array_elements on a field and joining against that. That's really not within the scope of SQLAlchemy or this list though, and you'll have better luck search (or asking) on Stack Overflow.  There are a handful of questions and solutions there on this topic.

Once you can figure out the PostgreSQL queries to accomplish what you want, this list can help you convert it to SQLAlchemy if you have trouble.

Simon King

unread,
Oct 19, 2021, 9:46:16 AMOct 19
to sqlal...@googlegroups.com
For what it's worth, I think the "?" operator would work for this with
JSONB, but not with JSON:

postgres=# select '["user1", "user2"]'::jsonb ? 'user1';
?column?
----------
t
(1 row)

postgres=# select '["user1", "user2"]'::jsonb ? 'user2';
?column?
----------
t
(1 row)

postgres=# select '["user1", "user2"]'::jsonb ? 'user3';
?column?
----------
f
(1 row)


SQLAlchemy surfaces the "?" operator as <column>.has_key

https://docs.sqlalchemy.org/en/14/dialects/postgresql.html#sqlalchemy.dialects.postgresql.JSONB.Comparator.has_key

https://github.com/sqlalchemy/sqlalchemy/blob/main/lib/sqlalchemy/dialects/postgresql/json.py#L33

Simon
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
> ---
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/8f1986f6-4a39-4cad-93f2-a8d1c392b4b2n%40googlegroups.com.

Jonathan Vanasco

unread,
Oct 25, 2021, 11:51:27 AMOct 25
to sqlalchemy

I believer some of the newer jsonb operators would work well for this too. But for json, sigh...
Reply all
Reply to author
Forward
0 new messages