2 SELECT '""'::json; -- OK.
8 SELECT $$''$$::json; -- ERROR, single quotes are not allowed
9 ERROR: invalid input syntax for type json
10 LINE 1: SELECT $$''$$::json;
12 DETAIL: line 1: Token "'" is invalid.
13 SELECT '"abc"'::json; -- OK
19 SELECT '"abc'::json; -- ERROR, quotes not closed
20 ERROR: invalid input syntax for type json
21 LINE 1: SELECT '"abc'::json;
23 DETAIL: line 1: Token ""abc" is invalid.
25 def"'::json; -- ERROR, unescaped newline in string constant
26 ERROR: invalid input syntax for type json
29 DETAIL: line 1: Character with value "0x0a" must be escaped.
30 SELECT '"\n\"\\"'::json; -- OK, legal escapes
36 SELECT '"\v"'::json; -- ERROR, not a valid JSON escape
37 ERROR: invalid input syntax for type json
38 LINE 1: SELECT '"\v"'::json;
40 DETAIL: line 1: Invalid escape "\v".
41 SELECT '"\u"'::json; -- ERROR, incomplete escape
42 ERROR: invalid input syntax for type json
43 LINE 1: SELECT '"\u"'::json;
45 DETAIL: line 1: "\u" must be followed by four hexadecimal digits.
46 SELECT '"\u00"'::json; -- ERROR, incomplete escape
47 ERROR: invalid input syntax for type json
48 LINE 1: SELECT '"\u00"'::json;
50 DETAIL: line 1: "\u" must be followed by four hexadecimal digits.
51 SELECT '"\u000g"'::json; -- ERROR, g is not a hex digit
52 ERROR: invalid input syntax for type json
53 LINE 1: SELECT '"\u000g"'::json;
55 DETAIL: line 1: "\u" must be followed by four hexadecimal digits.
56 SELECT '"\u0000"'::json; -- OK, legal escape
62 SELECT '"\uaBcD"'::json; -- OK, uppercase and lower case both OK
69 SELECT '1'::json; -- OK
75 SELECT '0'::json; -- OK
81 SELECT '01'::json; -- ERROR, not valid according to JSON spec
82 ERROR: invalid input syntax for type json
83 LINE 1: SELECT '01'::json;
85 DETAIL: line 1: Token "01" is invalid.
86 SELECT '0.1'::json; -- OK
92 SELECT '9223372036854775808'::json; -- OK, even though it's too large for int8
98 SELECT '1e100'::json; -- OK
104 SELECT '1.3e100'::json; -- OK
110 SELECT '1f2'::json; -- ERROR
111 ERROR: invalid input syntax for type json
112 LINE 1: SELECT '1f2'::json;
114 DETAIL: line 1: Token "1f2" is invalid.
115 SELECT '0.x1'::json; -- ERROR
116 ERROR: invalid input syntax for type json
117 LINE 1: SELECT '0.x1'::json;
119 DETAIL: line 1: Token "0.x1" is invalid.
120 SELECT '1.3ex100'::json; -- ERROR
121 ERROR: invalid input syntax for type json
122 LINE 1: SELECT '1.3ex100'::json;
124 DETAIL: line 1: Token "1.3ex100" is invalid.
126 SELECT '[]'::json; -- OK
132 SELECT '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'::json; -- OK
134 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
135 [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
138 SELECT '[1,2]'::json; -- OK
144 SELECT '[1,2,]'::json; -- ERROR, trailing comma
145 ERROR: invalid input syntax for type json: "[1,2,]"
146 LINE 1: SELECT '[1,2,]'::json;
148 DETAIL: line 1: Expected string, number, object, array, true, false, or null, but found "]".
149 SELECT '[1,2'::json; -- ERROR, no closing bracket
150 ERROR: invalid input syntax for type json: "[1,2"
151 LINE 1: SELECT '[1,2'::json;
153 DETAIL: The input string ended unexpectedly.
154 SELECT '[1,[2]'::json; -- ERROR, no closing bracket
155 ERROR: invalid input syntax for type json: "[1,[2]"
156 LINE 1: SELECT '[1,[2]'::json;
158 DETAIL: The input string ended unexpectedly.
160 SELECT '{}'::json; -- OK
166 SELECT '{"abc"}'::json; -- ERROR, no value
167 ERROR: invalid input syntax for type json: "{"abc"}"
168 LINE 1: SELECT '{"abc"}'::json;
170 DETAIL: line 1: Expected ":", but found "}".
171 SELECT '{"abc":1}'::json; -- OK
177 SELECT '{1:"abc"}'::json; -- ERROR, keys must be strings
178 ERROR: invalid input syntax for type json: "{1:"abc"}"
179 LINE 1: SELECT '{1:"abc"}'::json;
181 DETAIL: line 1: Expected string or "}", but found "1".
182 SELECT '{"abc",1}'::json; -- ERROR, wrong separator
183 ERROR: invalid input syntax for type json: "{"abc",1}"
184 LINE 1: SELECT '{"abc",1}'::json;
186 DETAIL: line 1: Expected ":", but found ",".
187 SELECT '{"abc"=1}'::json; -- ERROR, totally wrong separator
188 ERROR: invalid input syntax for type json
189 LINE 1: SELECT '{"abc"=1}'::json;
191 DETAIL: line 1: Token "=" is invalid.
192 SELECT '{"abc"::1}'::json; -- ERROR, another wrong separator
193 ERROR: invalid input syntax for type json: "{"abc"::1}"
194 LINE 1: SELECT '{"abc"::1}'::json;
196 DETAIL: line 1: Expected string, number, object, array, true, false, or null, but found ":".
197 SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::json; -- OK
199 ---------------------------------------------------------
200 {"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}
203 SELECT '{"abc":1:2}'::json; -- ERROR, colon in wrong spot
204 ERROR: invalid input syntax for type json: "{"abc":1:2}"
205 LINE 1: SELECT '{"abc":1:2}'::json;
207 DETAIL: line 1: Expected "," or "}", but found ":".
208 SELECT '{"abc":1,3}'::json; -- ERROR, no value
209 ERROR: invalid input syntax for type json: "{"abc":1,3}"
210 LINE 1: SELECT '{"abc":1,3}'::json;
212 DETAIL: line 1: Expected string, but found "3".
213 -- Miscellaneous stuff.
214 SELECT 'true'::json; -- OK
220 SELECT 'false'::json; -- OK
226 SELECT 'null'::json; -- OK
232 SELECT ' true '::json; -- OK, even with extra whitespace
238 SELECT 'true false'::json; -- ERROR, too many values
239 ERROR: invalid input syntax for type json: "true false"
240 LINE 1: SELECT 'true false'::json;
242 DETAIL: line 1: Expected end of input, but found "false".
243 SELECT 'true, false'::json; -- ERROR, too many values
244 ERROR: invalid input syntax for type json: "true, false"
245 LINE 1: SELECT 'true, false'::json;
247 DETAIL: line 1: Expected end of input, but found ",".
248 SELECT 'truf'::json; -- ERROR, not a keyword
249 ERROR: invalid input syntax for type json
250 LINE 1: SELECT 'truf'::json;
252 DETAIL: line 1: Token "truf" is invalid.
253 SELECT 'trues'::json; -- ERROR, not a keyword
254 ERROR: invalid input syntax for type json
255 LINE 1: SELECT 'trues'::json;
257 DETAIL: line 1: Token "trues" is invalid.
258 SELECT ''::json; -- ERROR, no value
259 ERROR: invalid input syntax for type json: ""
260 LINE 1: SELECT ''::json;
262 DETAIL: The input string ended unexpectedly.
263 SELECT ' '::json; -- ERROR, no value
264 ERROR: invalid input syntax for type json: " "
265 LINE 1: SELECT ' '::json;
267 DETAIL: The input string ended unexpectedly.
270 SELECT array_to_json(array(select 1 as a));
276 SELECT array_to_json(array_agg(q),false) from (select x as b, x * 2 as c from generate_series(1,3) x) q;
278 ---------------------------------------------
279 [{"b":1,"c":2},{"b":2,"c":4},{"b":3,"c":6}]
282 SELECT array_to_json(array_agg(q),true) from (select x as b, x * 2 as c from generate_series(1,3) x) q;
290 SELECT array_to_json(array_agg(q),false)
291 FROM ( SELECT $$a$$ || x AS b, y AS c,
292 ARRAY[ROW(x.*,ARRAY[1,2,3]),
293 ROW(y.*,ARRAY[4,5,6])] AS z
294 FROM generate_series(1,2) x,
295 generate_series(4,5) y) q;
297 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
298 [{"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]},{"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]},{"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]},{"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}]
301 SELECT array_to_json(array_agg(x),false) from generate_series(5,10) x;
307 SELECT array_to_json('{{1,5},{99,100}}'::int[]);
314 SELECT row_to_json(row(1,'foo'));
316 ---------------------
320 SELECT row_to_json(q)
321 FROM (SELECT $$a$$ || x AS b,
323 ARRAY[ROW(x.*,ARRAY[1,2,3]),
324 ROW(y.*,ARRAY[4,5,6])] AS z
325 FROM generate_series(1,2) x,
326 generate_series(4,5) y) q;
328 --------------------------------------------------------------------
329 {"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
330 {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
331 {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
332 {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
335 SELECT row_to_json(q,true)
336 FROM (SELECT $$a$$ || x AS b,
338 ARRAY[ROW(x.*,ARRAY[1,2,3]),
339 ROW(y.*,ARRAY[4,5,6])] AS z
340 FROM generate_series(1,2) x,
341 generate_series(4,5) y) q;
343 -----------------------------------------------------
346 "z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
349 "z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
352 "z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
355 "z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
358 CREATE TEMP TABLE rows AS
359 SELECT x, 'txt' || x as y
360 FROM generate_series(1,3) AS x;
361 SELECT row_to_json(q,true)
373 SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),false);
375 -----------------------
376 {"f1":[5,6,7,8,9,10]}
379 -- non-numeric output
380 SELECT row_to_json(q)
381 FROM (SELECT 'NaN'::float8 AS "float8field") q;
383 -----------------------
384 {"float8field":"NaN"}
387 SELECT row_to_json(q)
388 FROM (SELECT 'Infinity'::float8 AS "float8field") q;
390 ----------------------------
391 {"float8field":"Infinity"}
394 SELECT row_to_json(q)
395 FROM (SELECT '-Infinity'::float8 AS "float8field") q;
397 -----------------------------
398 {"float8field":"-Infinity"}
402 SELECT row_to_json(q)
403 FROM (SELECT '{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}'::json AS "jsonfield") q;
405 ------------------------------------------------------------------
406 {"jsonfield":{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}}