Skip to content
Advertisement

Update jsonb column with value from row_to_json()

I have a table containing a data that look like:

The column json_data is a column with jsonb type that contains some json that do not relate to anything which I want to update with row_to_json() function. The result should be something like this

which will get the result from row_to_json function to update each row. I am not sure how can I use the UPDATE query to do this.

Advertisement

Answer

Use the function to_jsonb() and the - operator to remove the column json_data from the resulting json object:

You can remove more than one column, e.g.:

Alternatively, you can use jsonb_build_object() instead of to_jsonb():

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