I have postgres table (let say my_table) containing JSON field (let say json_field) as:
(1, {"data": [{"val_a":1, "b":"somevalue"}, {"val_a":2, "b":"othervalue"}, ...]}),
(2, {"data": [{"val_a":22, "b":"somevalue2"}, {"val_a":12, "b":"othervalue2"}, ...]}),
....
(n, {"data": [{"val_a":11, "b":"somevaluen"}, {"val_a":122, "b":"othervaluen"}, ...]})
I want to return ARRAY - int[] of val_a values only.
I tried stored procedure (plpgsql), as follows:
declare
data_array json[];
vals float8[];
begin
SELECT json_field->'data' into data_array FROM my_table;
FOREACH i IN ARRAY data_array
vals := array_append(vals, i->'val_a')
-- nor array_append(vals, i->'val_a') works
END LOOP;
return vals;
end;
which gives me syntax error:
array_append(vals, i->'val_a').
What is the best way to extract val_a?
Using PostgreSQL 12.6