A json value cannot be persisted inside an array or a composite type. Both fail on INSERT with the same error, while the jsonb equivalents work:
ERROR: "json" serialization requires a string argument, got types.JSONDocument (SQLSTATE XX000)
CREATE TABLE ja (id int PRIMARY KEY, j json[]);
INSERT INTO ja VALUES (1, ARRAY['{"a":1}'::json]); -- ERROR
INSERT INTO ja VALUES (1, '{"{\"a\": 1}"}'); -- ERROR (array literal form fails too)
CREATE TYPE jrec AS (id int, j json);
CREATE TABLE jc (id int PRIMARY KEY, r jrec);
INSERT INTO jc VALUES (1, ROW(1, '{"a":1}')::jrec); -- ERROR
-- postgres accepts all three
It is specific to persistence — building the same values in an expression works, so the failure is in writing an element rather than in constructing it:
SELECT ARRAY['{"a":1}'::json]; -- works: {"{\"a\": 1}"}
And it is specific to json; the jsonb versions round-trip fine:
CREATE TABLE jba (id int PRIMARY KEY, j jsonb[]);
INSERT INTO jba VALUES (1, ARRAY['{"a":1}'::jsonb, '{"b":2}'::jsonb]);
SELECT j FROM jba; -- {"{\"a\": 1}","{\"b\": 2}"}
CREATE TYPE jbrec AS (id int, j jsonb);
CREATE TABLE jbc (id int PRIMARY KEY, r jbrec);
INSERT INTO jbc VALUES (1, ROW(1, '{"a":1}')::jbrec);
SELECT r FROM jbc; -- (1,"{\"a\": 1}")
Since nothing can currently be written through this path, there is no existing data in any format to stay compatible with.
A
jsonvalue cannot be persisted inside an array or a composite type. Both fail on INSERT with the same error, while thejsonbequivalents work:It is specific to persistence — building the same values in an expression works, so the failure is in writing an element rather than in constructing it:
And it is specific to
json; thejsonbversions round-trip fine:Since nothing can currently be written through this path, there is no existing data in any format to stay compatible with.