0

I have a json object field in PostgreSQL in mytable.fields as below

{
  "a": "1",
  "b": [
    {
      "c": 2,
      "d": 3
    },
    {
      "c": 4,
      "d": 5
    },
    {
      "c": 6,
      "d": 7
    }
  ]
}

How can I delete all "c" key and value pairs like as below? I found other StackOverflow solutions that remove the whole object, but I couldn't find this.

{
  "a": "1",
  "b": [
    {
      "d": 3
    },
    {
      "d": 5
    },
    {
      "d": 7
    }
  ]
}

1 Answer 1

1
with data as (
  select 
    your_json_col :: jsonb as obj,
    jsonb_array_elements((your_json_col ->> 'b') :: jsonb) :: jsonb - 'c' as new_obj
  from your_table)
select 
  obj || jsonb_build_object('b', jsonb_agg(new_obj))
from data
group by obj;
Sign up to request clarification or add additional context in comments.

1 Comment

Is it possible to keep other columns in this table and literally UPDATE only this json column into this table?

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.