I have the following nested types defined in postgres:
CREATE TYPE address AS ( name text, street text, zip text, city text, country text ); CREATE TYPE customer AS ( customer_number text, created timestamp WITH TIME ZONE, default_billing_address address, default_shipping_address address );
And now I would like to populate these types in a stored procedure that receives json as an input parameter. This works for fields at the top level, the output displays the internal format of the composite postgres type:
# select json_populate_record(null::customer, '{"customer_number":"12345678"}'::json)::customer; json_populate_record ---------------------- (12345678,,,) (1 row)
However, postgres does not handle the json nested structure:
# select json_populate_record(null::customer, '{"customer_number":"12345678","default_shipping_address":{"name":"","street":"","zip":"12345","city":"Berlin","country":"DE"}}'::json)::customer; ERROR: malformed record literal: "{"name":"","street":"","zip":"12345","city":"Berlin","country":"DE"}" DETAIL: Missing left parenthesis.
Which works again if the attached property is in the internal postgres format, for example here:
# select json_populate_record(null::customer, '{"customer_number":"12345678","default_shipping_address":"(\"\",\"\",12345,Berlin,DE)"}'::json)::customer; json_populate_record -------------------------------------------- (12345678,,,"("""","""",12345,Berlin,DE)") (1 row)
Is there a way to get postgres to convert from a nested json structure to the corresponding composite type?