0

I have an array of json objects, from which I want to removed three specific objects. For example:

[{"id": "aaa"}, {"id": "aab"}, {"id": "aac"}, {"id": "aad"}]

I need to update the table and remove from the array the objects that contain "aaa", "aab", and "aac" so that the array would only contain

[{"id": "aad"}]

or whatever other json objects that are stored in the array and don't contain the previous mentioned values stored in "id".

I'm using Postgres, and I know just that I need to use UPDATE, but that's about it (I'm still a beginner).

1 Answer 1

1

You need to unnest the array, filter out the unwanted elements and then aggregate back:

update the_table
   set the_column = (select jsonb_agg(j)
                     from jsonb_array_elements(the_table.the_column) as t(j)
                     where j ->> 'id' not in ('aaa','aab','aac'));

Online example: https://rextester.com/HCJK50138

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.