I have a json
(not jsonb
) column in a PostgreSQL (version 13.12) table from which I want to remove specific values of an array attribute.
For example, in the following table (db-fiddle) I want to remove all occurrences of "D" from the actions
attributes.
DROP TABLE IF EXISTS test;
CREATE TABLE test (
data JSON
);
INSERT INTO test VALUES
('{"name": "Alice", "actions": ["B", "C", "D"]}'),
('{"name": "Charles", "actions": ["C", "D", "E"]}');
SELECT * FROM test;
How can I do this with a query?
3
Answers
Output:
This query should do the trick, however, use a jsonb column if it’s feasible and get rid of all type castings in the query
Just select:
Update:
View on DB Fiddle
Core feature is to use the jsonb operator
-
:Using
jsonb
would make things a bit easier, no casting back and forth.