Postgres 12, column contains sample json from Quill Editor like:
{
"ops":
[
{
"insert": "Text 1"
},
{
"insert": {"video":"..."}
},
{
"insert": "Text 2"
},
{
"insert": {"image":"data:image/jpeg;base64,..."}
}]
}
To get result as Text 1 Text 2, meaning filter out (ignore/skip) all "insert": {"video":"..."} and "insert": {"image":"..."} if presented.
Operators like checking ::json ?| array['video', 'image'] or deleting ::jsonb-'video' seem helpful, but pg doesn't do one-line if-then-else (condition)?true_value:false_value. How to utilize them in this function?
CREATE OR REPLACE FUNCTION public.fun(
j json)
RETURNS text
LANGUAGE 'plpgsql' immutable
AS $BODY$
begin
return string_agg((obj->>'insert') || ' ', '')
FROM json_array_elements(j->'ops') obj;
end;
$BODY$;