Is there a better way to update multiple values in JSON?

We need to update multiple values inside a JSON-document.
The only way I could find doing this without pushing the whole thing was by nesting json_set calls:

UPDATE mytale SET data=json_set(json_set(json_set(data, '{field1}'::string[], :field1)::jsonb, '{field2}'::string[], :field2)::jsonb, '{field3}'::string[], :field3) WHERE id=:id

Is there a better way because this feels pretty awkward to do :slight_smile:

Hey @codepitbull,

Give this a try:

UPDATE mytale SET data = data || '{"field1": ..., "field2": ..., "field3": ...}'::jsonb

Let me know if youโ€™re able to update multiple values this way.

Thanks,
Matt

Hey, thanks for the quick reply.
I have already replaced all my nested json_set-calls.
Works like a charm :slight_smile:

No problem at all!

Happy you were able to figure it out.

Let me know if I can help out with anything else.

Best,
Matt