Thread: jsonb concatenation loses properties on nested objects

jsonb concatenation loses properties on nested objects

From
Phillip Haydon
Date:
I'm not sure if this is by design or not but I find it strange and believe
it's a bug.

If you concat two jsonb docs together, all first level properties are added
or modified.

Given:

select '{"FirstName": "Phillip"}'::jsonb || '{"LastName": "Haydon"}'::jsonb


You will get a result of:

{"LastName": "Haydon", "FirstName": "Phillip"}


However, if you have an object value with properties in that object that
differ between docs. The nested properties are lost.

select '{"User": {"FirstName": "Phillip"}}'::jsonb || '{"User":
{"LastName": "Haydon"}}'::jsonb


This results in:

{"User": {"LastName": "Haydon"}}


Based on the result of the first example I would have expected:

{"User": {"LastName": "Haydon", "FirstName": "Phillip"}}





Is this by design or is it a bug?


Thanks.


--

Kind Regards,
Phillip Haydon
www.philliphaydon.com

Re: jsonb concatenation loses properties on nested objects

From
Dmitry Dolgov
Date:
> Is this by design or is it a bug?

It's by design because `jsonb_concat` is a "shallow concatenation" [1].
Here is a related thread in hackers [2]. "Deep concatenation" functionality
is still to be implemented, but you can use `jsonb_deep_extend` from this
extension [3].

[1]
https://wiki.postgresql.org/wiki/What's_new_in_PostgreSQL_9.5#jsonb_.7C.7C_jsonb_.28concatenate_.2F_overwrite.29
[2]

https://www.postgresql.org/message-id/flat/CAM3SWZRcSp1dS0o5gXeepO7k4QxjjO%2Bh_NsRnCA%3Dmx-OEJ8vYQ%40mail.gmail.com#CAM3SWZRcSp1dS0o5gXeepO7k4QxjjO+h_NsRnCA=mx-OEJ8vYQ@mail.gmail.com
[3] https://www.postgresql.org/message-id/55006879.2050601@dunslane.net