Postgresql JSON object update.

63 views
Skip to first unread message

Massimiliano della Rovere

unread,
May 24, 2021, 11:12:09 AM5/24/21
to sqlal...@googlegroups.com
In postgresql the || operator is the only way (no, the concat() function doesn't work) to concat 2 JSONB dicts; note that this works only with JSONB and not JSON.

Example:

suppose column "t.c" contains '{"a": 1}'::jsonb

SELECT t.c || jsonb_build_object('b', 2);    

gives

 {"a": 1, "b": 2}

Question: How can I obtain the || operator in sqlalchemy (core, 1.3) to merge 2 JSONB dicts?


Simon King

unread,
May 24, 2021, 11:49:01 AM5/24/21
to sqlal...@googlegroups.com
You can use the "op" method:

https://docs.sqlalchemy.org/en/14/core/sqlelement.html#sqlalchemy.sql.expression.Operators.op

some_column.op("||")(other_column)

Hope that helps,

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/CADKhPGRmKtTvZa4jpWby67v7fO-xbO31oJm%3D8jpb%2BrKS5_t2WQ%40mail.gmail.com.

Massimiliano della Rovere

unread,
May 24, 2021, 11:55:57 AM5/24/21
to sqlal...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages