10

I have a json column called "Parameters" in a "Assets" table. The jsons are nested and have the following structure:

{
"params1": {"k": 1.1, "efficiency": 0.7}, 
"params2": {"k": 1.9, "C_max": 100.0, "C_min": 10.0}
}

I would like to update only one value "k" in "params2" and keep the rest of the values unchanged. I tried with the following code:

update "Assets" 
set "Parameters" = "Parameters"::jsonb || '{"params2":{"k":2.2}}'::jsonb
where "Id" = 185;

This results in

{
"params1": {"k": 1.1, "efficiency": 0.7}, 
"params2": {"k": 2.2}
}

So the rest of the parameters in "params2" are dropped which is not desired. What would be a way to modify "k" and leave the rest as it is?

0

1 Answer 1

21

You are looking for jsonb_set() (documentation)

update "Assets" 
  set "Parameters" = jsonb_set("Parameters"::jsonb, '{params2, k}', to_jsonb(2.2))
where ...
0

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.