1

I have a table xyz, with a metadata jsonb column in postgres.
Table : xyz column : metadata, type = jsonb

metadata = {"exceptions": {"first_exception": "first_value"} }

I want to add a new sub_attribute

desired metadata = {"exceptions": {"first_exception": "123"},{"second_exception": "234"}  }

I can use the

update xyz 
SET metadata = jsonb_set(metadata->'exceptions', '{second_exception}', '"234"', true). 

But I want to get the value 234 from a select query. I am not able to figure how to combine the select query with the update to do this.

2
  • to facilitate a bit, could add the create table statements? Commented Feb 27, 2018 at 17:01
  • I add the structure. I just want to know how to use query's output as a value for the new attribute to be added Commented Feb 27, 2018 at 18:56

1 Answer 1

3

You can do

UPDATE xyz
SET metadata = jsonb_set(metadata, '{exceptions, second_exception}', other.value::jsonb)
FROM other
WHERE other.column = xyz.column

Pay attention that {"exceptions": {"first_exception": "123"},{"second_exception": "234"}} is not a valid json and update will give you following result {"exceptions": {"first_exception": "123", "second_exception": "234"}}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.