From a025db2eb015444abf5101646dd764431008f07b Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Fri, 27 Nov 2026 01:14:05 -0500 Subject: [PATCH] Fix OMIT QUOTES for arrays and objects in JSON_QUERY and JSON_TABLE OMIT QUOTES is documented to affect only scalar strings, but arrays and objects were turned into text too. Reported-by: Chunling Qin <303677365@qq.com> Discussion: https://postgr.es/m/19824-6b112e27c335304a@postgresql.org --- src/backend/utils/adt/jsonfuncs.c | 5 +++-- src/test/regress/expected/sqljson_queryfuncs.out | 13 +++++++++++++ src/test/regress/sql/sqljson_queryfuncs.sql | 4 ++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c index 25f14f92370..cfd6af723ee 100644 --- a/src/backend/utils/adt/jsonfuncs.c +++ b/src/backend/utils/adt/jsonfuncs.c @@ -3383,9 +3383,10 @@ json_populate_type(Datum json_val, Oid json_type, jsv.val.jsonb = &jbv; - if (omit_quotes) + /* OMIT QUOTES applies only to scalars, not to arrays and objects. */ + if (omit_quotes && JB_ROOT_IS_SCALAR(jsonb)) { - char *str = JsonbUnquote(DatumGetJsonbP(json_val)); + char *str = JsonbUnquote(jsonb); /* fill the quote-stripped string */ jbv.type = jbvString; diff --git a/src/test/regress/expected/sqljson_queryfuncs.out b/src/test/regress/expected/sqljson_queryfuncs.out index 19db825c8ab..6bf09aeaffc 100644 --- a/src/test/regress/expected/sqljson_queryfuncs.out +++ b/src/test/regress/expected/sqljson_queryfuncs.out @@ -1109,6 +1109,19 @@ SELECT * FROM unnest((JSON_QUERY(jsonb '{"reca": [{"a": 1, "t": ["foo", []]}, {" 2 | | | [{}, true] | (2 rows) +-- OMIT QUOTES does not apply to arrays and objects +SELECT JSON_QUERY(jsonb '[1,2]', '$' RETURNING int[] OMIT QUOTES ERROR ON ERROR); + json_query +------------ + {1,2} +(1 row) + +SELECT JSON_QUERY(jsonb '{"a": 1, "t": "x"}', '$' RETURNING sqljsonb_rec OMIT QUOTES ERROR ON ERROR); + json_query +------------ + (1,x,,,) +(1 row) + SELECT JSON_QUERY(jsonb '[{"a": 1, "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]', '$[0]' RETURNING jsonpath); json_query ------------ diff --git a/src/test/regress/sql/sqljson_queryfuncs.sql b/src/test/regress/sql/sqljson_queryfuncs.sql index 5c675d98235..a1a6e357dbd 100644 --- a/src/test/regress/sql/sqljson_queryfuncs.sql +++ b/src/test/regress/sql/sqljson_queryfuncs.sql @@ -336,6 +336,10 @@ SELECT JSON_QUERY(jsonb '[{"a": "a", "b": "foo", "t": "aaa", "js": [1, "2", {}], SELECT * FROM unnest((JSON_QUERY(jsonb '{"jsa": [{"a": 1, "b": ["foo"]}, {"a": 2, "c": {}}, 123]}', '$' RETURNING sqljsonb_rec)).jsa); SELECT * FROM unnest((JSON_QUERY(jsonb '{"reca": [{"a": 1, "t": ["foo", []]}, {"a": 2, "jb": [{}, true]}]}', '$' RETURNING sqljsonb_reca)).reca); +-- OMIT QUOTES does not apply to arrays and objects +SELECT JSON_QUERY(jsonb '[1,2]', '$' RETURNING int[] OMIT QUOTES ERROR ON ERROR); +SELECT JSON_QUERY(jsonb '{"a": 1, "t": "x"}', '$' RETURNING sqljsonb_rec OMIT QUOTES ERROR ON ERROR); + SELECT JSON_QUERY(jsonb '[{"a": 1, "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]', '$[0]' RETURNING jsonpath); SELECT JSON_QUERY(jsonb '[{"a": 1, "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]', '$[0]' RETURNING jsonpath ERROR ON ERROR); -- 2.37.1 (Apple Git-137.1)