]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/json.out
Fix treatment of nulls in jsonb_agg and jsonb_object_agg
[postgresql] / src / test / regress / expected / json.out
1 -- Strings.
2 SELECT '""'::json;                              -- OK.
3  json 
4 ------
5  ""
6 (1 row)
7
8 SELECT $$''$$::json;                    -- ERROR, single quotes are not allowed
9 ERROR:  invalid input syntax for type json
10 LINE 1: SELECT $$''$$::json;
11                ^
12 DETAIL:  Token "'" is invalid.
13 CONTEXT:  JSON data, line 1: '...
14 SELECT '"abc"'::json;                   -- OK
15  json  
16 -------
17  "abc"
18 (1 row)
19
20 SELECT '"abc'::json;                    -- ERROR, quotes not closed
21 ERROR:  invalid input syntax for type json
22 LINE 1: SELECT '"abc'::json;
23                ^
24 DETAIL:  Token ""abc" is invalid.
25 CONTEXT:  JSON data, line 1: "abc
26 SELECT '"abc
27 def"'::json;                                    -- ERROR, unescaped newline in string constant
28 ERROR:  invalid input syntax for type json
29 LINE 1: SELECT '"abc
30                ^
31 DETAIL:  Character with value 0x0a must be escaped.
32 CONTEXT:  JSON data, line 1: "abc
33 SELECT '"\n\"\\"'::json;                -- OK, legal escapes
34    json   
35 ----------
36  "\n\"\\"
37 (1 row)
38
39 SELECT '"\v"'::json;                    -- ERROR, not a valid JSON escape
40 ERROR:  invalid input syntax for type json
41 LINE 1: SELECT '"\v"'::json;
42                ^
43 DETAIL:  Escape sequence "\v" is invalid.
44 CONTEXT:  JSON data, line 1: "\v...
45 SELECT '"\u"'::json;                    -- ERROR, incomplete escape
46 ERROR:  invalid input syntax for type json
47 LINE 1: SELECT '"\u"'::json;
48                ^
49 DETAIL:  "\u" must be followed by four hexadecimal digits.
50 CONTEXT:  JSON data, line 1: "\u"
51 SELECT '"\u00"'::json;                  -- ERROR, incomplete escape
52 ERROR:  invalid input syntax for type json
53 LINE 1: SELECT '"\u00"'::json;
54                ^
55 DETAIL:  "\u" must be followed by four hexadecimal digits.
56 CONTEXT:  JSON data, line 1: "\u00"
57 SELECT '"\u000g"'::json;                -- ERROR, g is not a hex digit
58 ERROR:  invalid input syntax for type json
59 LINE 1: SELECT '"\u000g"'::json;
60                ^
61 DETAIL:  "\u" must be followed by four hexadecimal digits.
62 CONTEXT:  JSON data, line 1: "\u000g...
63 SELECT '"\u0000"'::json;                -- OK, legal escape
64    json   
65 ----------
66  "\u0000"
67 (1 row)
68
69 SELECT '"\uaBcD"'::json;                -- OK, uppercase and lower case both OK
70    json   
71 ----------
72  "\uaBcD"
73 (1 row)
74
75 -- Numbers.
76 SELECT '1'::json;                               -- OK
77  json 
78 ------
79  1
80 (1 row)
81
82 SELECT '0'::json;                               -- OK
83  json 
84 ------
85  0
86 (1 row)
87
88 SELECT '01'::json;                              -- ERROR, not valid according to JSON spec
89 ERROR:  invalid input syntax for type json
90 LINE 1: SELECT '01'::json;
91                ^
92 DETAIL:  Token "01" is invalid.
93 CONTEXT:  JSON data, line 1: 01
94 SELECT '0.1'::json;                             -- OK
95  json 
96 ------
97  0.1
98 (1 row)
99
100 SELECT '9223372036854775808'::json;     -- OK, even though it's too large for int8
101         json         
102 ---------------------
103  9223372036854775808
104 (1 row)
105
106 SELECT '1e100'::json;                   -- OK
107  json  
108 -------
109  1e100
110 (1 row)
111
112 SELECT '1.3e100'::json;                 -- OK
113   json   
114 ---------
115  1.3e100
116 (1 row)
117
118 SELECT '1f2'::json;                             -- ERROR
119 ERROR:  invalid input syntax for type json
120 LINE 1: SELECT '1f2'::json;
121                ^
122 DETAIL:  Token "1f2" is invalid.
123 CONTEXT:  JSON data, line 1: 1f2
124 SELECT '0.x1'::json;                    -- ERROR
125 ERROR:  invalid input syntax for type json
126 LINE 1: SELECT '0.x1'::json;
127                ^
128 DETAIL:  Token "0.x1" is invalid.
129 CONTEXT:  JSON data, line 1: 0.x1
130 SELECT '1.3ex100'::json;                -- ERROR
131 ERROR:  invalid input syntax for type json
132 LINE 1: SELECT '1.3ex100'::json;
133                ^
134 DETAIL:  Token "1.3ex100" is invalid.
135 CONTEXT:  JSON data, line 1: 1.3ex100
136 -- Arrays.
137 SELECT '[]'::json;                              -- OK
138  json 
139 ------
140  []
141 (1 row)
142
143 SELECT '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'::json;  -- OK
144                                                                                                    json                                                                                                   
145 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
146  [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
147 (1 row)
148
149 SELECT '[1,2]'::json;                   -- OK
150  json  
151 -------
152  [1,2]
153 (1 row)
154
155 SELECT '[1,2,]'::json;                  -- ERROR, trailing comma
156 ERROR:  invalid input syntax for type json
157 LINE 1: SELECT '[1,2,]'::json;
158                ^
159 DETAIL:  Expected JSON value, but found "]".
160 CONTEXT:  JSON data, line 1: [1,2,]
161 SELECT '[1,2'::json;                    -- ERROR, no closing bracket
162 ERROR:  invalid input syntax for type json
163 LINE 1: SELECT '[1,2'::json;
164                ^
165 DETAIL:  The input string ended unexpectedly.
166 CONTEXT:  JSON data, line 1: [1,2
167 SELECT '[1,[2]'::json;                  -- ERROR, no closing bracket
168 ERROR:  invalid input syntax for type json
169 LINE 1: SELECT '[1,[2]'::json;
170                ^
171 DETAIL:  The input string ended unexpectedly.
172 CONTEXT:  JSON data, line 1: [1,[2]
173 -- Objects.
174 SELECT '{}'::json;                              -- OK
175  json 
176 ------
177  {}
178 (1 row)
179
180 SELECT '{"abc"}'::json;                 -- ERROR, no value
181 ERROR:  invalid input syntax for type json
182 LINE 1: SELECT '{"abc"}'::json;
183                ^
184 DETAIL:  Expected ":", but found "}".
185 CONTEXT:  JSON data, line 1: {"abc"}
186 SELECT '{"abc":1}'::json;               -- OK
187    json    
188 -----------
189  {"abc":1}
190 (1 row)
191
192 SELECT '{1:"abc"}'::json;               -- ERROR, keys must be strings
193 ERROR:  invalid input syntax for type json
194 LINE 1: SELECT '{1:"abc"}'::json;
195                ^
196 DETAIL:  Expected string or "}", but found "1".
197 CONTEXT:  JSON data, line 1: {1...
198 SELECT '{"abc",1}'::json;               -- ERROR, wrong separator
199 ERROR:  invalid input syntax for type json
200 LINE 1: SELECT '{"abc",1}'::json;
201                ^
202 DETAIL:  Expected ":", but found ",".
203 CONTEXT:  JSON data, line 1: {"abc",...
204 SELECT '{"abc"=1}'::json;               -- ERROR, totally wrong separator
205 ERROR:  invalid input syntax for type json
206 LINE 1: SELECT '{"abc"=1}'::json;
207                ^
208 DETAIL:  Token "=" is invalid.
209 CONTEXT:  JSON data, line 1: {"abc"=...
210 SELECT '{"abc"::1}'::json;              -- ERROR, another wrong separator
211 ERROR:  invalid input syntax for type json
212 LINE 1: SELECT '{"abc"::1}'::json;
213                ^
214 DETAIL:  Expected JSON value, but found ":".
215 CONTEXT:  JSON data, line 1: {"abc"::...
216 SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::json; -- OK
217                           json                           
218 ---------------------------------------------------------
219  {"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}
220 (1 row)
221
222 SELECT '{"abc":1:2}'::json;             -- ERROR, colon in wrong spot
223 ERROR:  invalid input syntax for type json
224 LINE 1: SELECT '{"abc":1:2}'::json;
225                ^
226 DETAIL:  Expected "," or "}", but found ":".
227 CONTEXT:  JSON data, line 1: {"abc":1:...
228 SELECT '{"abc":1,3}'::json;             -- ERROR, no value
229 ERROR:  invalid input syntax for type json
230 LINE 1: SELECT '{"abc":1,3}'::json;
231                ^
232 DETAIL:  Expected string, but found "3".
233 CONTEXT:  JSON data, line 1: {"abc":1,3...
234 -- Miscellaneous stuff.
235 SELECT 'true'::json;                    -- OK
236  json 
237 ------
238  true
239 (1 row)
240
241 SELECT 'false'::json;                   -- OK
242  json  
243 -------
244  false
245 (1 row)
246
247 SELECT 'null'::json;                    -- OK
248  json 
249 ------
250  null
251 (1 row)
252
253 SELECT ' true '::json;                  -- OK, even with extra whitespace
254   json  
255 --------
256   true 
257 (1 row)
258
259 SELECT 'true false'::json;              -- ERROR, too many values
260 ERROR:  invalid input syntax for type json
261 LINE 1: SELECT 'true false'::json;
262                ^
263 DETAIL:  Expected end of input, but found "false".
264 CONTEXT:  JSON data, line 1: true false
265 SELECT 'true, false'::json;             -- ERROR, too many values
266 ERROR:  invalid input syntax for type json
267 LINE 1: SELECT 'true, false'::json;
268                ^
269 DETAIL:  Expected end of input, but found ",".
270 CONTEXT:  JSON data, line 1: true,...
271 SELECT 'truf'::json;                    -- ERROR, not a keyword
272 ERROR:  invalid input syntax for type json
273 LINE 1: SELECT 'truf'::json;
274                ^
275 DETAIL:  Token "truf" is invalid.
276 CONTEXT:  JSON data, line 1: truf
277 SELECT 'trues'::json;                   -- ERROR, not a keyword
278 ERROR:  invalid input syntax for type json
279 LINE 1: SELECT 'trues'::json;
280                ^
281 DETAIL:  Token "trues" is invalid.
282 CONTEXT:  JSON data, line 1: trues
283 SELECT ''::json;                                -- ERROR, no value
284 ERROR:  invalid input syntax for type json
285 LINE 1: SELECT ''::json;
286                ^
287 DETAIL:  The input string ended unexpectedly.
288 CONTEXT:  JSON data, line 1: 
289 SELECT '    '::json;                    -- ERROR, no value
290 ERROR:  invalid input syntax for type json
291 LINE 1: SELECT '    '::json;
292                ^
293 DETAIL:  The input string ended unexpectedly.
294 CONTEXT:  JSON data, line 1:     
295 --constructors
296 -- array_to_json
297 SELECT array_to_json(array(select 1 as a));
298  array_to_json 
299 ---------------
300  [1]
301 (1 row)
302
303 SELECT array_to_json(array_agg(q),false) from (select x as b, x * 2 as c from generate_series(1,3) x) q;
304                 array_to_json                
305 ---------------------------------------------
306  [{"b":1,"c":2},{"b":2,"c":4},{"b":3,"c":6}]
307 (1 row)
308
309 SELECT array_to_json(array_agg(q),true) from (select x as b, x * 2 as c from generate_series(1,3) x) q;
310   array_to_json  
311 -----------------
312  [{"b":1,"c":2},+
313   {"b":2,"c":4},+
314   {"b":3,"c":6}]
315 (1 row)
316
317 SELECT array_to_json(array_agg(q),false)
318   FROM ( SELECT $$a$$ || x AS b, y AS c,
319                ARRAY[ROW(x.*,ARRAY[1,2,3]),
320                ROW(y.*,ARRAY[4,5,6])] AS z
321          FROM generate_series(1,2) x,
322               generate_series(4,5) y) q;
323                                                                                                                                  array_to_json                                                                                                                                 
324 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
325  [{"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]}]}]
326 (1 row)
327
328 SELECT array_to_json(array_agg(x),false) from generate_series(5,10) x;
329  array_to_json  
330 ----------------
331  [5,6,7,8,9,10]
332 (1 row)
333
334 SELECT array_to_json('{{1,5},{99,100}}'::int[]);
335   array_to_json   
336 ------------------
337  [[1,5],[99,100]]
338 (1 row)
339
340 -- row_to_json
341 SELECT row_to_json(row(1,'foo'));
342      row_to_json     
343 ---------------------
344  {"f1":1,"f2":"foo"}
345 (1 row)
346
347 SELECT row_to_json(q)
348 FROM (SELECT $$a$$ || x AS b,
349          y AS c,
350          ARRAY[ROW(x.*,ARRAY[1,2,3]),
351                ROW(y.*,ARRAY[4,5,6])] AS z
352       FROM generate_series(1,2) x,
353            generate_series(4,5) y) q;
354                             row_to_json                             
355 --------------------------------------------------------------------
356  {"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
357  {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
358  {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
359  {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
360 (4 rows)
361
362 SELECT row_to_json(q,true)
363 FROM (SELECT $$a$$ || x AS b,
364          y AS c,
365          ARRAY[ROW(x.*,ARRAY[1,2,3]),
366                ROW(y.*,ARRAY[4,5,6])] AS z
367       FROM generate_series(1,2) x,
368            generate_series(4,5) y) q;
369                      row_to_json                     
370 -----------------------------------------------------
371  {"b":"a1",                                         +
372   "c":4,                                            +
373   "z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
374  {"b":"a1",                                         +
375   "c":5,                                            +
376   "z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
377  {"b":"a2",                                         +
378   "c":4,                                            +
379   "z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
380  {"b":"a2",                                         +
381   "c":5,                                            +
382   "z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
383 (4 rows)
384
385 CREATE TEMP TABLE rows AS
386 SELECT x, 'txt' || x as y
387 FROM generate_series(1,3) AS x;
388 SELECT row_to_json(q,true)
389 FROM rows q;
390  row_to_json  
391 --------------
392  {"x":1,     +
393   "y":"txt1"}
394  {"x":2,     +
395   "y":"txt2"}
396  {"x":3,     +
397   "y":"txt3"}
398 (3 rows)
399
400 SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),false);
401       row_to_json      
402 -----------------------
403  {"f1":[5,6,7,8,9,10]}
404 (1 row)
405
406 -- to_json, timestamps
407 select to_json(timestamp '2014-05-28 12:22:35.614298');
408            to_json            
409 ------------------------------
410  "2014-05-28T12:22:35.614298"
411 (1 row)
412
413 BEGIN;
414 SET LOCAL TIME ZONE 10.5;
415 select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
416               to_json               
417 ------------------------------------
418  "2014-05-29T02:52:35.614298+10:30"
419 (1 row)
420
421 SET LOCAL TIME ZONE -8;
422 select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
423               to_json               
424 ------------------------------------
425  "2014-05-28T08:22:35.614298-08:00"
426 (1 row)
427
428 COMMIT;
429 select to_json(date '2014-05-28');
430    to_json    
431 --------------
432  "2014-05-28"
433 (1 row)
434
435 select to_json(date 'Infinity');
436   to_json   
437 ------------
438  "infinity"
439 (1 row)
440
441 select to_json(timestamp 'Infinity');
442   to_json   
443 ------------
444  "infinity"
445 (1 row)
446
447 select to_json(timestamptz 'Infinity');
448   to_json   
449 ------------
450  "infinity"
451 (1 row)
452
453 --json_agg
454 SELECT json_agg(q)
455   FROM ( SELECT $$a$$ || x AS b, y AS c,
456                ARRAY[ROW(x.*,ARRAY[1,2,3]),
457                ROW(y.*,ARRAY[4,5,6])] AS z
458          FROM generate_series(1,2) x,
459               generate_series(4,5) y) q;
460                                json_agg                                
461 -----------------------------------------------------------------------
462  [{"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, +
463   {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}, +
464   {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, +
465   {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}]
466 (1 row)
467
468 SELECT json_agg(q ORDER BY x, y)
469   FROM rows q;
470        json_agg        
471 -----------------------
472  [{"x":1,"y":"txt1"}, +
473   {"x":2,"y":"txt2"}, +
474   {"x":3,"y":"txt3"}]
475 (1 row)
476
477 UPDATE rows SET x = NULL WHERE x = 1;
478 SELECT json_agg(q ORDER BY x NULLS FIRST, y)
479   FROM rows q;
480          json_agg         
481 --------------------------
482  [{"x":null,"y":"txt1"}, +
483   {"x":2,"y":"txt2"},    +
484   {"x":3,"y":"txt3"}]
485 (1 row)
486
487 -- non-numeric output
488 SELECT row_to_json(q)
489 FROM (SELECT 'NaN'::float8 AS "float8field") q;
490       row_to_json      
491 -----------------------
492  {"float8field":"NaN"}
493 (1 row)
494
495 SELECT row_to_json(q)
496 FROM (SELECT 'Infinity'::float8 AS "float8field") q;
497         row_to_json         
498 ----------------------------
499  {"float8field":"Infinity"}
500 (1 row)
501
502 SELECT row_to_json(q)
503 FROM (SELECT '-Infinity'::float8 AS "float8field") q;
504          row_to_json         
505 -----------------------------
506  {"float8field":"-Infinity"}
507 (1 row)
508
509 -- json input
510 SELECT row_to_json(q)
511 FROM (SELECT '{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}'::json AS "jsonfield") q;
512                            row_to_json                            
513 ------------------------------------------------------------------
514  {"jsonfield":{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}}
515 (1 row)
516
517 -- json extraction functions
518 CREATE TEMP TABLE test_json (
519        json_type text,
520        test_json json
521 );
522 INSERT INTO test_json VALUES
523 ('scalar','"a scalar"'),
524 ('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
525 ('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
526 SELECT test_json -> 'x'
527 FROM test_json
528 WHERE json_type = 'scalar';
529  ?column? 
530 ----------
531  
532 (1 row)
533
534 SELECT test_json -> 'x'
535 FROM test_json
536 WHERE json_type = 'array';
537  ?column? 
538 ----------
539  
540 (1 row)
541
542 SELECT test_json -> 'x'
543 FROM test_json
544 WHERE json_type = 'object';
545  ?column? 
546 ----------
547  
548 (1 row)
549
550 SELECT test_json->'field2'
551 FROM test_json
552 WHERE json_type = 'object';
553  ?column? 
554 ----------
555  "val2"
556 (1 row)
557
558 SELECT test_json->>'field2'
559 FROM test_json
560 WHERE json_type = 'object';
561  ?column? 
562 ----------
563  val2
564 (1 row)
565
566 SELECT test_json -> 2
567 FROM test_json
568 WHERE json_type = 'scalar';
569  ?column? 
570 ----------
571  
572 (1 row)
573
574 SELECT test_json -> 2
575 FROM test_json
576 WHERE json_type = 'array';
577  ?column? 
578 ----------
579  "two"
580 (1 row)
581
582 SELECT test_json -> -1
583 FROM test_json
584 WHERE json_type = 'array';
585  ?column? 
586 ----------
587  {"f1":9}
588 (1 row)
589
590 SELECT test_json -> 2
591 FROM test_json
592 WHERE json_type = 'object';
593  ?column? 
594 ----------
595  
596 (1 row)
597
598 SELECT test_json->>2
599 FROM test_json
600 WHERE json_type = 'array';
601  ?column? 
602 ----------
603  two
604 (1 row)
605
606 SELECT test_json ->> 6 FROM test_json WHERE json_type = 'array';
607  ?column? 
608 ----------
609  [1,2,3]
610 (1 row)
611
612 SELECT test_json ->> 7 FROM test_json WHERE json_type = 'array';
613  ?column? 
614 ----------
615  {"f1":9}
616 (1 row)
617
618 SELECT test_json ->> 'field4' FROM test_json WHERE json_type = 'object';
619  ?column? 
620 ----------
621  4
622 (1 row)
623
624 SELECT test_json ->> 'field5' FROM test_json WHERE json_type = 'object';
625  ?column? 
626 ----------
627  [1,2,3]
628 (1 row)
629
630 SELECT test_json ->> 'field6' FROM test_json WHERE json_type = 'object';
631  ?column? 
632 ----------
633  {"f1":9}
634 (1 row)
635
636 SELECT json_object_keys(test_json)
637 FROM test_json
638 WHERE json_type = 'scalar';
639 ERROR:  cannot call json_object_keys on a scalar
640 SELECT json_object_keys(test_json)
641 FROM test_json
642 WHERE json_type = 'array';
643 ERROR:  cannot call json_object_keys on an array
644 SELECT json_object_keys(test_json)
645 FROM test_json
646 WHERE json_type = 'object';
647  json_object_keys 
648 ------------------
649  field1
650  field2
651  field3
652  field4
653  field5
654  field6
655 (6 rows)
656
657 -- test extending object_keys resultset - initial resultset size is 256
658 select count(*) from
659     (select json_object_keys(json_object(array_agg(g)))
660      from (select unnest(array['f'||n,n::text])as g
661            from generate_series(1,300) as n) x ) y;
662  count 
663 -------
664    300
665 (1 row)
666
667 -- nulls
668 select (test_json->'field3') is null as expect_false
669 from test_json
670 where json_type = 'object';
671  expect_false 
672 --------------
673  f
674 (1 row)
675
676 select (test_json->>'field3') is null as expect_true
677 from test_json
678 where json_type = 'object';
679  expect_true 
680 -------------
681  t
682 (1 row)
683
684 select (test_json->3) is null as expect_false
685 from test_json
686 where json_type = 'array';
687  expect_false 
688 --------------
689  f
690 (1 row)
691
692 select (test_json->>3) is null as expect_true
693 from test_json
694 where json_type = 'array';
695  expect_true 
696 -------------
697  t
698 (1 row)
699
700 -- corner cases
701 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::text;
702  ?column? 
703 ----------
704  
705 (1 row)
706
707 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int;
708  ?column? 
709 ----------
710  
711 (1 row)
712
713 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 1;
714  ?column? 
715 ----------
716  
717 (1 row)
718
719 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> -1;
720  ?column? 
721 ----------
722  
723 (1 row)
724
725 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
726  ?column? 
727 ----------
728  
729 (1 row)
730
731 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> '';
732  ?column? 
733 ----------
734  
735 (1 row)
736
737 select '[{"b": "c"}, {"b": "cc"}]'::json -> 1;
738   ?column?   
739 -------------
740  {"b": "cc"}
741 (1 row)
742
743 select '[{"b": "c"}, {"b": "cc"}]'::json -> 3;
744  ?column? 
745 ----------
746  
747 (1 row)
748
749 select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z';
750  ?column? 
751 ----------
752  
753 (1 row)
754
755 select '{"a": "c", "b": null}'::json -> 'b';
756  ?column? 
757 ----------
758  null
759 (1 row)
760
761 select '"foo"'::json -> 1;
762  ?column? 
763 ----------
764  
765 (1 row)
766
767 select '"foo"'::json -> 'z';
768  ?column? 
769 ----------
770  
771 (1 row)
772
773 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
774  ?column? 
775 ----------
776  
777 (1 row)
778
779 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
780  ?column? 
781 ----------
782  
783 (1 row)
784
785 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1;
786  ?column? 
787 ----------
788  
789 (1 row)
790
791 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
792  ?column? 
793 ----------
794  
795 (1 row)
796
797 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> '';
798  ?column? 
799 ----------
800  
801 (1 row)
802
803 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 1;
804   ?column?   
805 -------------
806  {"b": "cc"}
807 (1 row)
808
809 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
810  ?column? 
811 ----------
812  
813 (1 row)
814
815 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z';
816  ?column? 
817 ----------
818  
819 (1 row)
820
821 select '{"a": "c", "b": null}'::json ->> 'b';
822  ?column? 
823 ----------
824  
825 (1 row)
826
827 select '"foo"'::json ->> 1;
828  ?column? 
829 ----------
830  
831 (1 row)
832
833 select '"foo"'::json ->> 'z';
834  ?column? 
835 ----------
836  
837 (1 row)
838
839 -- array length
840 SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
841  json_array_length 
842 -------------------
843                  5
844 (1 row)
845
846 SELECT json_array_length('[]');
847  json_array_length 
848 -------------------
849                  0
850 (1 row)
851
852 SELECT json_array_length('{"f1":1,"f2":[5,6]}');
853 ERROR:  cannot get array length of a non-array
854 SELECT json_array_length('4');
855 ERROR:  cannot get array length of a scalar
856 -- each
857 select json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}');
858      json_each     
859 -------------------
860  (f1,"[1,2,3]")
861  (f2,"{""f3"":1}")
862  (f4,null)
863 (3 rows)
864
865 select * from json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
866  key |   value   
867 -----+-----------
868  f1  | [1,2,3]
869  f2  | {"f3":1}
870  f4  | null
871  f5  | 99
872  f6  | "stringy"
873 (5 rows)
874
875 select json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}');
876   json_each_text   
877 -------------------
878  (f1,"[1,2,3]")
879  (f2,"{""f3"":1}")
880  (f4,)
881  (f5,null)
882 (4 rows)
883
884 select * from json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
885  key |  value   
886 -----+----------
887  f1  | [1,2,3]
888  f2  | {"f3":1}
889  f4  | 
890  f5  | 99
891  f6  | stringy
892 (5 rows)
893
894 -- extract_path, extract_path_as_text
895 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
896  json_extract_path 
897 -------------------
898  "stringy"
899 (1 row)
900
901 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
902  json_extract_path 
903 -------------------
904  {"f3":1}
905 (1 row)
906
907 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
908  json_extract_path 
909 -------------------
910  "f3"
911 (1 row)
912
913 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
914  json_extract_path 
915 -------------------
916  1
917 (1 row)
918
919 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
920  json_extract_path_text 
921 ------------------------
922  stringy
923 (1 row)
924
925 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
926  json_extract_path_text 
927 ------------------------
928  {"f3":1}
929 (1 row)
930
931 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
932  json_extract_path_text 
933 ------------------------
934  f3
935 (1 row)
936
937 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
938  json_extract_path_text 
939 ------------------------
940  1
941 (1 row)
942
943 -- extract_path nulls
944 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_false;
945  expect_false 
946 --------------
947  f
948 (1 row)
949
950 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_true;
951  expect_true 
952 -------------
953  t
954 (1 row)
955
956 select json_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_false;
957  expect_false 
958 --------------
959  f
960 (1 row)
961
962 select json_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_true;
963  expect_true 
964 -------------
965  t
966 (1 row)
967
968 -- extract_path operators
969 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f4','f6'];
970  ?column?  
971 -----------
972  "stringy"
973 (1 row)
974
975 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2'];
976  ?column? 
977 ----------
978  {"f3":1}
979 (1 row)
980
981 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','0'];
982  ?column? 
983 ----------
984  "f3"
985 (1 row)
986
987 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','1'];
988  ?column? 
989 ----------
990  1
991 (1 row)
992
993 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f4','f6'];
994  ?column? 
995 ----------
996  stringy
997 (1 row)
998
999 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2'];
1000  ?column? 
1001 ----------
1002  {"f3":1}
1003 (1 row)
1004
1005 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','0'];
1006  ?column? 
1007 ----------
1008  f3
1009 (1 row)
1010
1011 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
1012  ?column? 
1013 ----------
1014  1
1015 (1 row)
1016
1017 -- corner cases for same
1018 select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
1019          ?column?          
1020 ---------------------------
1021  {"a": {"b":{"c": "foo"}}}
1022 (1 row)
1023
1024 select '[1,2,3]'::json #> '{}';
1025  ?column? 
1026 ----------
1027  [1,2,3]
1028 (1 row)
1029
1030 select '"foo"'::json #> '{}';
1031  ?column? 
1032 ----------
1033  "foo"
1034 (1 row)
1035
1036 select '42'::json #> '{}';
1037  ?column? 
1038 ----------
1039  42
1040 (1 row)
1041
1042 select 'null'::json #> '{}';
1043  ?column? 
1044 ----------
1045  null
1046 (1 row)
1047
1048 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
1049       ?column?      
1050 --------------------
1051  {"b":{"c": "foo"}}
1052 (1 row)
1053
1054 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null];
1055  ?column? 
1056 ----------
1057  
1058 (1 row)
1059
1060 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', ''];
1061  ?column? 
1062 ----------
1063  
1064 (1 row)
1065
1066 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
1067    ?column?   
1068 --------------
1069  {"c": "foo"}
1070 (1 row)
1071
1072 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c'];
1073  ?column? 
1074 ----------
1075  "foo"
1076 (1 row)
1077
1078 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c','d'];
1079  ?column? 
1080 ----------
1081  
1082 (1 row)
1083
1084 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','z','c'];
1085  ?column? 
1086 ----------
1087  
1088 (1 row)
1089
1090 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','1','b'];
1091  ?column? 
1092 ----------
1093  "cc"
1094 (1 row)
1095
1096 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','z','b'];
1097  ?column? 
1098 ----------
1099  
1100 (1 row)
1101
1102 select '[{"b": "c"}, {"b": "cc"}]'::json #> array['1','b'];
1103  ?column? 
1104 ----------
1105  "cc"
1106 (1 row)
1107
1108 select '[{"b": "c"}, {"b": "cc"}]'::json #> array['z','b'];
1109  ?column? 
1110 ----------
1111  
1112 (1 row)
1113
1114 select '[{"b": "c"}, {"b": null}]'::json #> array['1','b'];
1115  ?column? 
1116 ----------
1117  null
1118 (1 row)
1119
1120 select '"foo"'::json #> array['z'];
1121  ?column? 
1122 ----------
1123  
1124 (1 row)
1125
1126 select '42'::json #> array['f2'];
1127  ?column? 
1128 ----------
1129  
1130 (1 row)
1131
1132 select '42'::json #> array['0'];
1133  ?column? 
1134 ----------
1135  
1136 (1 row)
1137
1138 select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}';
1139          ?column?          
1140 ---------------------------
1141  {"a": {"b":{"c": "foo"}}}
1142 (1 row)
1143
1144 select '[1,2,3]'::json #>> '{}';
1145  ?column? 
1146 ----------
1147  [1,2,3]
1148 (1 row)
1149
1150 select '"foo"'::json #>> '{}';
1151  ?column? 
1152 ----------
1153  foo
1154 (1 row)
1155
1156 select '42'::json #>> '{}';
1157  ?column? 
1158 ----------
1159  42
1160 (1 row)
1161
1162 select 'null'::json #>> '{}';
1163  ?column? 
1164 ----------
1165  
1166 (1 row)
1167
1168 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
1169       ?column?      
1170 --------------------
1171  {"b":{"c": "foo"}}
1172 (1 row)
1173
1174 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null];
1175  ?column? 
1176 ----------
1177  
1178 (1 row)
1179
1180 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', ''];
1181  ?column? 
1182 ----------
1183  
1184 (1 row)
1185
1186 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
1187    ?column?   
1188 --------------
1189  {"c": "foo"}
1190 (1 row)
1191
1192 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c'];
1193  ?column? 
1194 ----------
1195  foo
1196 (1 row)
1197
1198 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c','d'];
1199  ?column? 
1200 ----------
1201  
1202 (1 row)
1203
1204 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','z','c'];
1205  ?column? 
1206 ----------
1207  
1208 (1 row)
1209
1210 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','1','b'];
1211  ?column? 
1212 ----------
1213  cc
1214 (1 row)
1215
1216 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','z','b'];
1217  ?column? 
1218 ----------
1219  
1220 (1 row)
1221
1222 select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['1','b'];
1223  ?column? 
1224 ----------
1225  cc
1226 (1 row)
1227
1228 select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','b'];
1229  ?column? 
1230 ----------
1231  
1232 (1 row)
1233
1234 select '[{"b": "c"}, {"b": null}]'::json #>> array['1','b'];
1235  ?column? 
1236 ----------
1237  
1238 (1 row)
1239
1240 select '"foo"'::json #>> array['z'];
1241  ?column? 
1242 ----------
1243  
1244 (1 row)
1245
1246 select '42'::json #>> array['f2'];
1247  ?column? 
1248 ----------
1249  
1250 (1 row)
1251
1252 select '42'::json #>> array['0'];
1253  ?column? 
1254 ----------
1255  
1256 (1 row)
1257
1258 -- array_elements
1259 select json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
1260   json_array_elements  
1261 -----------------------
1262  1
1263  true
1264  [1,[2,3]]
1265  null
1266  {"f1":1,"f2":[7,8,9]}
1267  false
1268  "stringy"
1269 (7 rows)
1270
1271 select * from json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
1272          value         
1273 -----------------------
1274  1
1275  true
1276  [1,[2,3]]
1277  null
1278  {"f1":1,"f2":[7,8,9]}
1279  false
1280  "stringy"
1281 (7 rows)
1282
1283 select json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
1284  json_array_elements_text 
1285 --------------------------
1286  1
1287  true
1288  [1,[2,3]]
1289  
1290  {"f1":1,"f2":[7,8,9]}
1291  false
1292  stringy
1293 (7 rows)
1294
1295 select * from json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
1296          value         
1297 -----------------------
1298  1
1299  true
1300  [1,[2,3]]
1301  
1302  {"f1":1,"f2":[7,8,9]}
1303  false
1304  stringy
1305 (7 rows)
1306
1307 -- populate_record
1308 create type jpop as (a text, b int, c timestamp);
1309 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1310    a    | b | c 
1311 --------+---+---
1312  blurfl |   | 
1313 (1 row)
1314
1315 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1316    a    | b |            c             
1317 --------+---+--------------------------
1318  blurfl | 3 | Mon Dec 31 15:30:56 2012
1319 (1 row)
1320
1321 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1322    a    | b | c 
1323 --------+---+---
1324  blurfl |   | 
1325 (1 row)
1326
1327 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1328    a    | b |            c             
1329 --------+---+--------------------------
1330  blurfl | 3 | Mon Dec 31 15:30:56 2012
1331 (1 row)
1332
1333 select * from json_populate_record(null::jpop,'{"a":[100,200,false],"x":43.2}') q;
1334         a        | b | c 
1335 -----------------+---+---
1336  [100,200,false] |   | 
1337 (1 row)
1338
1339 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":[100,200,false],"x":43.2}') q;
1340         a        | b |            c             
1341 -----------------+---+--------------------------
1342  [100,200,false] | 3 | Mon Dec 31 15:30:56 2012
1343 (1 row)
1344
1345 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"c":[100,200,false],"x":43.2}') q;
1346 ERROR:  invalid input syntax for type timestamp: "[100,200,false]"
1347 -- populate_recordset
1348 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1349    a    | b |            c             
1350 --------+---+--------------------------
1351  blurfl |   | 
1352         | 3 | Fri Jan 20 10:42:53 2012
1353 (2 rows)
1354
1355 select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1356    a    | b  |            c             
1357 --------+----+--------------------------
1358  blurfl | 99 | 
1359  def    |  3 | Fri Jan 20 10:42:53 2012
1360 (2 rows)
1361
1362 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1363    a    | b |            c             
1364 --------+---+--------------------------
1365  blurfl |   | 
1366         | 3 | Fri Jan 20 10:42:53 2012
1367 (2 rows)
1368
1369 select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1370    a    | b  |            c             
1371 --------+----+--------------------------
1372  blurfl | 99 | 
1373  def    |  3 | Fri Jan 20 10:42:53 2012
1374 (2 rows)
1375
1376 select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
1377        a       | b  |            c             
1378 ---------------+----+--------------------------
1379  [100,200,300] | 99 | 
1380  {"z":true}    |  3 | Fri Jan 20 10:42:53 2012
1381 (2 rows)
1382
1383 select * from json_populate_recordset(row('def',99,null)::jpop,'[{"c":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
1384 ERROR:  invalid input syntax for type timestamp: "[100,200,300]"
1385 create type jpop2 as (a int, b json, c int, d int);
1386 select * from json_populate_recordset(null::jpop2, '[{"a":2,"c":3,"b":{"z":4},"d":6}]') q;
1387  a |    b    | c | d 
1388 ---+---------+---+---
1389  2 | {"z":4} | 3 | 6
1390 (1 row)
1391
1392 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1393    a    | b |            c             
1394 --------+---+--------------------------
1395  blurfl |   | 
1396         | 3 | Fri Jan 20 10:42:53 2012
1397 (2 rows)
1398
1399 select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1400    a    | b  |            c             
1401 --------+----+--------------------------
1402  blurfl | 99 | 
1403  def    |  3 | Fri Jan 20 10:42:53 2012
1404 (2 rows)
1405
1406 select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
1407        a       | b  |            c             
1408 ---------------+----+--------------------------
1409  [100,200,300] | 99 | 
1410  {"z":true}    |  3 | Fri Jan 20 10:42:53 2012
1411 (2 rows)
1412
1413 -- handling of unicode surrogate pairs
1414 select json '{ "a":  "\ud83d\ude04\ud83d\udc36" }' -> 'a' as correct_in_utf8;
1415       correct_in_utf8       
1416 ----------------------------
1417  "\ud83d\ude04\ud83d\udc36"
1418 (1 row)
1419
1420 select json '{ "a":  "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row
1421 ERROR:  invalid input syntax for type json
1422 DETAIL:  Unicode high surrogate must not follow a high surrogate.
1423 CONTEXT:  JSON data, line 1: { "a":...
1424 select json '{ "a":  "\ude04\ud83d" }' -> 'a'; -- surrogates in wrong order
1425 ERROR:  invalid input syntax for type json
1426 DETAIL:  Unicode low surrogate must follow a high surrogate.
1427 CONTEXT:  JSON data, line 1: { "a":...
1428 select json '{ "a":  "\ud83dX" }' -> 'a'; -- orphan high surrogate
1429 ERROR:  invalid input syntax for type json
1430 DETAIL:  Unicode low surrogate must follow a high surrogate.
1431 CONTEXT:  JSON data, line 1: { "a":...
1432 select json '{ "a":  "\ude04X" }' -> 'a'; -- orphan low surrogate
1433 ERROR:  invalid input syntax for type json
1434 DETAIL:  Unicode low surrogate must follow a high surrogate.
1435 CONTEXT:  JSON data, line 1: { "a":...
1436 --handling of simple unicode escapes
1437 select json '{ "a":  "the Copyright \u00a9 sign" }' as correct_in_utf8;
1438             correct_in_utf8            
1439 ---------------------------------------
1440  { "a":  "the Copyright \u00a9 sign" }
1441 (1 row)
1442
1443 select json '{ "a":  "dollar \u0024 character" }' as correct_everywhere;
1444          correct_everywhere          
1445 -------------------------------------
1446  { "a":  "dollar \u0024 character" }
1447 (1 row)
1448
1449 select json '{ "a":  "dollar \\u0024 character" }' as not_an_escape;
1450             not_an_escape             
1451 --------------------------------------
1452  { "a":  "dollar \\u0024 character" }
1453 (1 row)
1454
1455 select json '{ "a":  "null \u0000 escape" }' as not_unescaped;
1456          not_unescaped          
1457 --------------------------------
1458  { "a":  "null \u0000 escape" }
1459 (1 row)
1460
1461 select json '{ "a":  "null \\u0000 escape" }' as not_an_escape;
1462           not_an_escape          
1463 ---------------------------------
1464  { "a":  "null \\u0000 escape" }
1465 (1 row)
1466
1467 select json '{ "a":  "the Copyright \u00a9 sign" }' ->> 'a' as correct_in_utf8;
1468    correct_in_utf8    
1469 ----------------------
1470  the Copyright Â© sign
1471 (1 row)
1472
1473 select json '{ "a":  "dollar \u0024 character" }' ->> 'a' as correct_everywhere;
1474  correct_everywhere 
1475 --------------------
1476  dollar $ character
1477 (1 row)
1478
1479 select json '{ "a":  "dollar \\u0024 character" }' ->> 'a' as not_an_escape;
1480       not_an_escape      
1481 -------------------------
1482  dollar \u0024 character
1483 (1 row)
1484
1485 select json '{ "a":  "null \u0000 escape" }' ->> 'a' as fails;
1486 ERROR:  unsupported Unicode escape sequence
1487 DETAIL:  \u0000 cannot be converted to text.
1488 CONTEXT:  JSON data, line 1: { "a":...
1489 select json '{ "a":  "null \\u0000 escape" }' ->> 'a' as not_an_escape;
1490    not_an_escape    
1491 --------------------
1492  null \u0000 escape
1493 (1 row)
1494
1495 --json_typeof() function
1496 select value, json_typeof(value)
1497   from (values (json '123.4'),
1498                (json '-1'),
1499                (json '"foo"'),
1500                (json 'true'),
1501                (json 'false'),
1502                (json 'null'),
1503                (json '[1, 2, 3]'),
1504                (json '[]'),
1505                (json '{"x":"foo", "y":123}'),
1506                (json '{}'),
1507                (NULL::json))
1508       as data(value);
1509         value         | json_typeof 
1510 ----------------------+-------------
1511  123.4                | number
1512  -1                   | number
1513  "foo"                | string
1514  true                 | boolean
1515  false                | boolean
1516  null                 | null
1517  [1, 2, 3]            | array
1518  []                   | array
1519  {"x":"foo", "y":123} | object
1520  {}                   | object
1521                       | 
1522 (11 rows)
1523
1524 -- json_build_array, json_build_object, json_object_agg
1525 SELECT json_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1526                            json_build_array                            
1527 -----------------------------------------------------------------------
1528  ["a", 1, "b", 1.2, "c", true, "d", null, "e", {"x": 3, "y": [1,2,3]}]
1529 (1 row)
1530
1531 SELECT json_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1532                              json_build_object                              
1533 ----------------------------------------------------------------------------
1534  {"a" : 1, "b" : 1.2, "c" : true, "d" : null, "e" : {"x": 3, "y": [1,2,3]}}
1535 (1 row)
1536
1537 SELECT json_build_object(
1538        'a', json_build_object('b',false,'c',99),
1539        'd', json_build_object('e',array[9,8,7]::int[],
1540            'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r)));
1541                                         json_build_object                                        
1542 -------------------------------------------------------------------------------------------------
1543  {"a" : {"b" : false, "c" : 99}, "d" : {"e" : [9,8,7], "f" : {"relkind":"r","name":"pg_class"}}}
1544 (1 row)
1545
1546 -- empty objects/arrays
1547 SELECT json_build_array();
1548  json_build_array 
1549 ------------------
1550  []
1551 (1 row)
1552
1553 SELECT json_build_object();
1554  json_build_object 
1555 -------------------
1556  {}
1557 (1 row)
1558
1559 -- make sure keys are quoted
1560 SELECT json_build_object(1,2);
1561  json_build_object 
1562 -------------------
1563  {"1" : 2}
1564 (1 row)
1565
1566 -- keys must be scalar and not null
1567 SELECT json_build_object(null,2);
1568 ERROR:  argument 1 cannot be null
1569 HINT:  Object keys should be text.
1570 SELECT json_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
1571 ERROR:  key value must be scalar, not array, composite, or json
1572 SELECT json_build_object(json '{"a":1,"b":2}', 3);
1573 ERROR:  key value must be scalar, not array, composite, or json
1574 SELECT json_build_object('{1,2,3}'::int[], 3);
1575 ERROR:  key value must be scalar, not array, composite, or json
1576 CREATE TEMP TABLE foo (serial_num int, name text, type text);
1577 INSERT INTO foo VALUES (847001,'t15','GE1043');
1578 INSERT INTO foo VALUES (847002,'t16','GE1043');
1579 INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
1580 SELECT json_build_object('turbines',json_object_agg(serial_num,json_build_object('name',name,'type',type)))
1581 FROM foo;
1582                                                                             json_build_object                                                                            
1583 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1584  {"turbines" : { "847001" : {"name" : "t15", "type" : "GE1043"}, "847002" : {"name" : "t16", "type" : "GE1043"}, "847003" : {"name" : "sub-alpha", "type" : "GESS90"} }}
1585 (1 row)
1586
1587 SELECT json_object_agg(name, type) FROM foo;
1588                         json_object_agg                         
1589 ----------------------------------------------------------------
1590  { "t15" : "GE1043", "t16" : "GE1043", "sub-alpha" : "GESS90" }
1591 (1 row)
1592
1593 INSERT INTO foo VALUES (999999, NULL, 'bar');
1594 SELECT json_object_agg(name, type) FROM foo;
1595 ERROR:  field name must not be null
1596 -- json_object
1597 -- one dimension
1598 SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
1599                       json_object                      
1600 -------------------------------------------------------
1601  {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1602 (1 row)
1603
1604 -- same but with two dimensions
1605 SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
1606                       json_object                      
1607 -------------------------------------------------------
1608  {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1609 (1 row)
1610
1611 -- odd number error
1612 SELECT json_object('{a,b,c}');
1613 ERROR:  array must have even number of elements
1614 -- one column error
1615 SELECT json_object('{{a},{b}}');
1616 ERROR:  array must have two columns
1617 -- too many columns error
1618 SELECT json_object('{{a,b,c},{b,c,d}}');
1619 ERROR:  array must have two columns
1620 -- too many dimensions error
1621 SELECT json_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
1622 ERROR:  wrong number of array subscripts
1623 --two argument form of json_object
1624 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
1625                      json_object                      
1626 ------------------------------------------------------
1627  {"a" : "1", "b" : "2", "c" : "3", "d e f" : "a b c"}
1628 (1 row)
1629
1630 -- too many dimensions
1631 SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}', '{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
1632 ERROR:  wrong number of array subscripts
1633 -- mismatched dimensions
1634 select json_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
1635 ERROR:  mismatched array dimensions
1636 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
1637 ERROR:  mismatched array dimensions
1638 -- null key error
1639 select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
1640 ERROR:  null value not allowed for object key
1641 -- empty key is allowed
1642 select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
1643                      json_object                     
1644 -----------------------------------------------------
1645  {"a" : "1", "b" : "2", "" : "3", "d e f" : "a b c"}
1646 (1 row)
1647
1648 -- json_to_record and json_to_recordset
1649 select * from json_to_record('{"a":1,"b":"foo","c":"bar"}')
1650     as x(a int, b text, d text);
1651  a |  b  | d 
1652 ---+-----+---
1653  1 | foo | 
1654 (1 row)
1655
1656 select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]')
1657     as x(a int, b text, c boolean);
1658  a |  b  | c 
1659 ---+-----+---
1660  1 | foo | 
1661  2 | bar | t
1662 (2 rows)
1663
1664 select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":false,"b":{"d":"bar"}}]')
1665     as x(a int, b json, c boolean);
1666  a |      b      | c 
1667 ---+-------------+---
1668  1 | {"d":"foo"} | t
1669  2 | {"d":"bar"} | f
1670 (2 rows)
1671
1672 -- json_strip_nulls
1673 select json_strip_nulls(null);
1674  json_strip_nulls 
1675 ------------------
1676  
1677 (1 row)
1678
1679 select json_strip_nulls('1');
1680  json_strip_nulls 
1681 ------------------
1682  1
1683 (1 row)
1684
1685 select json_strip_nulls('"a string"');
1686  json_strip_nulls 
1687 ------------------
1688  "a string"
1689 (1 row)
1690
1691 select json_strip_nulls('null');
1692  json_strip_nulls 
1693 ------------------
1694  null
1695 (1 row)
1696
1697 select json_strip_nulls('[1,2,null,3,4]');
1698  json_strip_nulls 
1699 ------------------
1700  [1,2,null,3,4]
1701 (1 row)
1702
1703 select json_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
1704           json_strip_nulls          
1705 ------------------------------------
1706  {"a":1,"c":[2,null,3],"d":{"e":4}}
1707 (1 row)
1708
1709 select json_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
1710   json_strip_nulls   
1711 ---------------------
1712  [1,{"a":1,"c":2},3]
1713 (1 row)
1714
1715 -- an empty object is not null and should not be stripped
1716 select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
1717  json_strip_nulls 
1718 ------------------
1719  {"a":{},"d":{}}
1720 (1 row)
1721