I am using postgres 12.X, I have the following logs table :-
logs
id bigint , jsondata text
Following are values in json
[
{
"loginfo": "somelog1",
"id": "app1"
},
{
"loginfo": "somelog2",
"id": "app2"
}
]
I need remove all json objects from this text column where id == "app2"
tried the below as per answer here :- https://dba.stackexchange.com/questions/84472/how-to-remove-object-from-json-array
UPDATE logs i SET jsondata = i2.jsondata FROM (SELECT id, array_to_json(array_agg(elem)) AS jsondata
FROM logs cand
, json_array_elements(cand.jsondata) elem
WHERE cand.jsondata @> '{[{"id":"app2"}]}'::jsonb
AND elem->>'id' <> 'app2'
GROUP BY 1 ) i2
WHERE i2.id = i.id;
But ERROR comes as below :-
ERROR: function json_array_elements(text) does not exist
jsonb(or at leastjson) so you need to cast it, e.g.json_array_elements(jsondata::json)- the best solution is to change its data type fromtexttojsonbusingalter table(and then use the jsonb_xxxx functions)