Skip to content

Commit 4ebe351

Browse files
committed
Allow empty string object keys in json_object().
This makes the behaviour consistent with the json parser, other json-generating functions, and the JSON standards.
1 parent d7cdf6e commit 4ebe351

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

src/backend/utils/adt/json.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,10 +2184,6 @@ json_object(PG_FUNCTION_ARGS)
21842184
errmsg("null value not allowed for object key")));
21852185

21862186
v = TextDatumGetCString(in_datums[i * 2]);
2187-
if (v[0] == '\0')
2188-
ereport(ERROR,
2189-
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2190-
errmsg("empty value not allowed for object key")));
21912187
if (i > 0)
21922188
appendStringInfoString(&result, ", ");
21932189
escape_json(&result, v);
@@ -2272,10 +2268,6 @@ json_object_two_arg(PG_FUNCTION_ARGS)
22722268
errmsg("null value not allowed for object key")));
22732269

22742270
v = TextDatumGetCString(key_datums[i]);
2275-
if (v[0] == '\0')
2276-
ereport(ERROR,
2277-
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2278-
errmsg("empty value not allowed for object key")));
22792271
if (i > 0)
22802272
appendStringInfoString(&result, ", ");
22812273
escape_json(&result, v);

src/test/regress/expected/json.out

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,9 +1213,13 @@ ERROR: mismatched array dimensions
12131213
-- null key error
12141214
select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
12151215
ERROR: null value not allowed for object key
1216-
-- empty key error
1216+
-- empty key is allowed
12171217
select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
1218-
ERROR: empty value not allowed for object key
1218+
json_object
1219+
-----------------------------------------------------
1220+
{"a" : "1", "b" : "2", "" : "3", "d e f" : "a b c"}
1221+
(1 row)
1222+
12191223
-- json_to_record and json_to_recordset
12201224
select * from json_to_record('{"a":1,"b":"foo","c":"bar"}')
12211225
as x(a int, b text, d text);

src/test/regress/expected/json_1.out

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,9 +1209,13 @@ ERROR: mismatched array dimensions
12091209
-- null key error
12101210
select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
12111211
ERROR: null value not allowed for object key
1212-
-- empty key error
1212+
-- empty key is allowed
12131213
select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
1214-
ERROR: empty value not allowed for object key
1214+
json_object
1215+
-----------------------------------------------------
1216+
{"a" : "1", "b" : "2", "" : "3", "d e f" : "a b c"}
1217+
(1 row)
1218+
12151219
-- json_to_record and json_to_recordset
12161220
select * from json_to_record('{"a":1,"b":"foo","c":"bar"}')
12171221
as x(a int, b text, d text);

src/test/regress/sql/json.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
435435

436436
select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
437437

438-
-- empty key error
438+
-- empty key is allowed
439439

440440
select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
441441

0 commit comments

Comments
 (0)