2

I have two scenarios that I am trying to figure out. I have a jsonb column in a table with a structure like this:

   {
    "1": {
        "a": 0.084,
        "b": 0.084,
        "c": 0.084
    },
    "2": {
        "a": 0.078,
        "b": 0.0814,
        "c": 0.078
    },
    "3": {
        "a": 0.0928,
        "b": 0.0975,
        "c": 0.0975
    }
   }

If I wanted to change the value of "3":{"b":} how would I go about doing it? I tried looking at functions like jsonb_set(), but that seemed like I would have to copy the entire jsonb object and re-paste it in every time I wanted to update one value.

The second issue I want to solve is if I want to delete the entire "3":{} object, the only solution I can seem to find is using jsonb_set() again.

Thanks for your help!

1 Answer 1

4

You can use jsonb_set() function with the related path '{3,b}' in order to update :

SELECT jsonb_set(jsonb_data, '{3,b}','0.1') 
  FROM tab

where 0.1 is just a sample value

Use #- operator in order to delete :

SELECT jsonb_data #- '{3}'
  FROM tab

Demo

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

2 Comments

Thanks! That works out really well and does exactly what I need it to. Is it possible to do that with multiple values in the same object? Like if I wanted to set the value for A, B and C at the same time? Thanks again.
you're welcome @ChristopherCarlisle. Not possible with multiple values within the same object.

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.