Skip to content
Advertisement

Update key of object inside array of objects in jsonb column

I have a jsonb column called data. It is deeply nested and there’s a key which’s value is an array of objects:

As you see, there are different forms of "b" key.

My goal is to update all rows with "b:" and "b_" keys and set them to "b".

Advertisement

Answer

This answer describes the way to rename an attribute of a json object. You can create a function based on this idea:

and another function to facilitate modifications of elements of a json array:

Use the function to update the table:

Test it in db<>fiddle.

Exemplary trigger before insert:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement