snippetsqlModerate
How to remove object from json array?
Viewed 0 times
fromarrayremovehowjsonobject
Problem
My table:
images format:
I need something like this:
CREATE TABLE items (
id BIGINT PRIMARY KEY NOT NULL,
name VARCHAR,
images json
);images format:
[
{
"id": "owner",
"full": "",
"thumb": ""
},
{
"id": "note_0",
"full": "",
"thumb": ""
},
{
"id": "note_1",
"full": "",
"thumb": ""
},
{
"id": "note_2",
"full": "",
"thumb": ""
}
]I need something like this:
UPDATE items SET images = delete(images, 'note_1');Solution
To remove all elements from the column
pg 9.3
SQL Fiddle.
Explain
pg 9.4
This gets much easier with
Eliminates unaffected rows at the start, which is much faster. Plus, there is extensive native index support for
Here are some example, benchmarks and comparison of new features to old json and MongoDB, plus outlook to jsquery by (some of) their main authors, Alexander Korotkov, Oleg Bartunov andTeodor Sigaevat PGCon 2014:
images (holding a json array) where 'id' is 'note_1':pg 9.3
UPDATE items i
SET images = i2.images
FROM (
SELECT id, array_to_json(array_agg(elem)) AS images
FROM items i2
, json_array_elements(i2.images) elem
WHERE elem->>'id' <> 'note_1'
GROUP BY 1
) i2
WHERE i2.id = i.id
AND json_array_length(i2.images) < json_array_length(i.images);SQL Fiddle.
Explain
- Unnest the JSON array with
json_array_elements()in a subquery using an implicitJOIN LATERALfor the set-returning function. Details:
- PostgreSQL joining using JSONB
- How to turn json array into postgres array?
- JOIN to the base table and add another
WHEREcondition usingjson_array_length()to exclude unaffected rows - so you don't update each and every row of the table, which would be expensive (and wide-spread) nonsense.
pg 9.4
This gets much easier with
jsonb and additional jsonb operators.UPDATE items i
SET images = i2.images
FROM (
SELECT id, array_to_json(array_agg(elem)) AS images
FROM items cand
, json_array_elements(cand.images) elem
WHERE cand.images @> '{[{"id":"note_1"}]}'::jsonb
AND elem->>'id' <> 'note_1'
GROUP BY 1
) i2
WHERE i2.id = i.id;Eliminates unaffected rows at the start, which is much faster. Plus, there is extensive native index support for
jsonb, too, now.Here are some example, benchmarks and comparison of new features to old json and MongoDB, plus outlook to jsquery by (some of) their main authors, Alexander Korotkov, Oleg Bartunov andTeodor Sigaevat PGCon 2014:
- "CREATE INDEX ... USING VODKA"
Code Snippets
UPDATE items i
SET images = i2.images
FROM (
SELECT id, array_to_json(array_agg(elem)) AS images
FROM items i2
, json_array_elements(i2.images) elem
WHERE elem->>'id' <> 'note_1'
GROUP BY 1
) i2
WHERE i2.id = i.id
AND json_array_length(i2.images) < json_array_length(i.images);UPDATE items i
SET images = i2.images
FROM (
SELECT id, array_to_json(array_agg(elem)) AS images
FROM items cand
, json_array_elements(cand.images) elem
WHERE cand.images @> '{[{"id":"note_1"}]}'::jsonb
AND elem->>'id' <> 'note_1'
GROUP BY 1
) i2
WHERE i2.id = i.id;Context
StackExchange Database Administrators Q#84472, answer score: 11
Revisions (0)
No revisions yet.