This illustrates the issue nicely:
When column b is of type text, and not an array, the following works:
select *
from json_to_record('{"a":1,"b":["hello", "There"],"c":"bar"}')
as x(a int, b text, d text);
a | b | d
---+--------------------+---
1 | ["hello", "There"] |
But if I define the b
column as an array, I get this error:
select *
from json_to_record('{"a":1,"b":["hello", "There"],"c":"bar"}')
as x(a int, b text[], d text)
ERROR: malformed array literal: "["hello", "There"]"
DETAIL: "[" must introduce explicitly-specified array dimensions.
How can I convince/coerce json_to_record
(or json_populate_record
) to convert a JSON array into the Postgres array of the target column type?