2

I have json list in table like:

[{ "a": 1, "b": 2, "c": 3 }, {"a":1, "b":2, "a":3}]

And i want remove all "a" keys. How i can do it?

I tried this:

UPDATE mytable
SET list = list::jsonb - 'a'

But it is not working.

I want this result:

[{"b": 2, "c": 3 }, {"b":2}] 
6
  • List column type is json Commented Jan 21, 2020 at 8:34
  • Can you add , {"a";3} to the input data, and also adjust the expected result (if needed.) Commented Jan 21, 2020 at 8:36
  • Yes, I add this. Commented Jan 21, 2020 at 8:44
  • May I edit? (I wasn't clear enough.) Commented Jan 21, 2020 at 8:45
  • Okey (if you can). Commented Jan 21, 2020 at 8:50

1 Answer 1

2

This will do it.

UPDATE mytable
SET list=(
  SELECT jsonb_agg(elem - 'a')::json
  FROM jsonb_array_elements(list::jsonb) elem
)
Sign up to request clarification or add additional context in comments.

2 Comments

"function jsonb_array_elements(json) does not exist"
Welcome. I was working with jsonb field on first post and edited for json when realizing.

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.