I am fairly new to SQL and wondering how to remove a specific element from the JSONB data below:
[
{"id":101,"content":"lorem ipsum", "username":"user_1"},
{"id":102,"content":"lorem ipsum", "username":"user_2"},
{"id":103,"content":"lorem ipsum", "username":"user_3"}
]
In this case I want to remove the object with id: 102
, so that the end result looks like this:
Expected Result
[
{"id":101,"content":"lorem ipsum", "username":"user_1"},
{"id":103,"content":"lorem ipsum", "username":"user_3"}
]
I am looking to write a PostgreSQL function that receives the id as parameter and returns the expected result above.
Any ideas on doing this with PostgreSQL?
I am working with PostgreSQL version 15.1 on Supabase.
I tried messing around with functions like jsonb_array_elements
, but none of these helped me, or I understood these wrongly. Any help is appreciated, Thank you!
3
Answers
read more about PL/pgSQL:
https://www.postgresqltutorial.com/postgresql-plpgsql/
brief summary:
it creates function
remove_jsonb_object
, takesjsonb_data
andtarget_id
as input parameters. And returns new jsonb data, but without target_id.Function:
You can use this function:
result output:
The following idea might likely not be the shortest and not the simplest possible, but is very clear and easy to understand.
See here the working example according to your data: db<>fiddle
So we use
JSONB_ARRAY_ELEMENTS
within a CTE to split the data in rows. Then we filter out the part with id = 102 because it should be removed.Then we use
JSON_AGG
to put the remaining parts together again.So, this will do:
As mentioned above, the linked fiddle show this, we can check every step there.
Of course, you can put this in any function you like, the 102 can be set as parameter.
This will be the function then:
I added another fiddle that shows the function is also working correctly.
See here: db<>fiddle
Please note this answer assumes you already stored the jsonb data which should be modified as a column in a table.
If this is not the case, I wouldn’t use a SQL function for such purposes, rather do that logic in your application. This should even be considered if you do store it in a column. Generally spoken, this kind of tasks is often be better done without SQL.
To remove a specific element from a JSONB array in PostgreSQL, you can use the
jsonb_array_elements()
function along with thejsonb_agg()
function. Here’s an example of a PostgreSQL function that receives anid
parameter and returns the JSONB array with the specified object removed:This function takes an
id_to_remove
parameter, which specifies theid
of the object to be removed. It uses thejsonb_array_elements()
function to expand the JSONB array into individual elements. Then, it filters out the element with a matchingid_to_remove
value. Finally, it usesjsonb_agg()
to aggregate the remaining elements back into a JSONB array and returns the result.You can call this function like this:
This will return the expected result:
Please note that you may need to adjust the JSONB array input in the function according to your actual data.