3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements tests for JSON SQL functions extension to the
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
18 do_execsql_test json101-1.1.00 {
19 SELECT json_array(1,2.5,null,'hello');
20 } {[1,2.5,null,"hello"]}
21 do_execsql_test json101-1.1.01 {
22 SELECT json_array(1,'{"abc":2.5,"def":null,"ghi":hello}',99);
23 -- the second term goes in as a string:
24 } {[1,"{\\"abc\\":2.5,\\"def\\":null,\\"ghi\\":hello}",99]}
25 do_execsql_test json101-1.1.02 {
26 SELECT json_array(1,json('{"abc":2.5,"def":null,"ghi":"hello"}'),99);
27 -- the second term goes in as JSON
28 } {[1,{"abc":2.5,"def":null,"ghi":"hello"},99]}
29 do_execsql_test json101-1.1.03 {
30 SELECT json_array(1,json_object('abc',2.5,'def',null,'ghi','hello'),99);
31 -- the second term goes in as JSON
32 } {[1,{"abc":2.5,"def":null,"ghi":"hello"},99]}
33 do_execsql_test json101-1.2 {
34 SELECT hex(json_array('String "\ Test'));
35 } {5B22537472696E67205C225C5C2054657374225D}
36 do_catchsql_test json101-1.3 {
37 SELECT json_array(1,printf('%.1000c','x'),x'abcd',3);
38 } {1 {JSON cannot hold BLOB values}}
39 do_execsql_test json101-1.4 {
40 SELECT json_array(-9223372036854775808,9223372036854775807,0,1,-1,
41 0.0, 1.0, -1.0, -1e99, +2e100,
43 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
44 19, NULL, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
45 'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ',
46 'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ',
47 'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ',
49 } {[-9223372036854775808,9223372036854775807,0,1,-1,0.0,1.0,-1.0,-1.0e+99,2.0e+100,"one","two","three",4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,null,21,22,23,24,25,26,27,28,29,30,31,"abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ",99]}
51 do_execsql_test json101-2.1 {
52 SELECT json_object('a',1,'b',2.5,'c',null,'d','String Test');
53 } {{{"a":1,"b":2.5,"c":null,"d":"String Test"}}}
54 do_catchsql_test json101-2.2 {
55 SELECT json_object('a',printf('%.1000c','x'),2,2.5);
56 } {1 {json_object() labels must be TEXT}}
57 do_catchsql_test json101-2.3 {
58 SELECT json_object('a',1,'b');
59 } {1 {json_object() requires an even number of arguments}}
60 do_catchsql_test json101-2.4 {
61 SELECT json_object('a',printf('%.1000c','x'),'b',x'abcd');
62 } {1 {JSON cannot hold BLOB values}}
64 do_execsql_test json101-3.1 {
65 SELECT json_replace('{"a":1,"b":2}','$.a','[3,4,5]');
66 } {{{"a":"[3,4,5]","b":2}}}
67 do_execsql_test json101-3.2 {
68 SELECT json_replace('{"a":1,"b":2}','$.a',json('[3,4,5]'));
69 } {{{"a":[3,4,5],"b":2}}}
70 do_execsql_test json101-3.3 {
71 SELECT json_type(json_set('{"a":1,"b":2}','$.b','{"x":3,"y":4}'),'$.b');
73 do_execsql_test json101-3.4 {
74 SELECT json_type(json_set('{"a":1,"b":2}','$.b',json('{"x":3,"y":4}')),'$.b');
77 do_execsql_test json101-3.5 {
78 SELECT fullkey, atom, '|' FROM json_tree(json_set('{}','$.x',123,'$.x',456));
79 } {{$} {} | {$.x} 456 |}
82 # Per rfc7159, any JSON value is allowed at the top level, and whitespace
83 # is permitting before and/or after that value.
85 do_execsql_test json101-4.1 {
88 VALUES('true'),('false'),('null'),('123'),('-234'),('34.5e+6'),
89 ('""'),('"\""'),('"\\"'),('"abcdefghijlmnopqrstuvwxyz"'),
90 ('[]'),('{}'),('[true,false,null,123,-234,34.5e+6,{},[]]'),
91 ('{"a":true,"b":{"c":false}}');
92 SELECT * FROM j1 WHERE NOT json_valid(x);
94 do_execsql_test json101-4.2 {
95 SELECT * FROM j1 WHERE NOT json_valid(char(0x20,0x09,0x0a,0x0d)||x);
97 do_execsql_test json101-4.3 {
98 SELECT * FROM j1 WHERE NOT json_valid(x||char(0x20,0x09,0x0a,0x0d));
101 # But an empty string, or a string of pure whitespace is not valid JSON.
103 do_execsql_test json101-4.4 {
104 SELECT json_valid(''), json_valid(char(0x20,0x09,0x0a,0x0d));
107 # json_remove() and similar functions with no edit operations return their
110 do_execsql_test json101-4.5 {
111 SELECT x FROM j1 WHERE json_remove(x)<>x;
113 do_execsql_test json101-4.6 {
114 SELECT x FROM j1 WHERE json_replace(x)<>x;
116 do_execsql_test json101-4.7 {
117 SELECT x FROM j1 WHERE json_set(x)<>x;
119 do_execsql_test json101-4.8 {
120 SELECT x FROM j1 WHERE json_insert(x)<>x;
123 # json_extract(JSON,'$') will return objects and arrays without change.
125 do_execsql_test json101-4.10 {
126 SELECT count(*) FROM j1 WHERE json_type(x) IN ('object','array');
128 WHERE json_extract(x,'$')<>x
129 AND json_type(x) IN ('object','array');
132 do_execsql_test json101-5.1 {
133 CREATE TABLE j2(id INTEGER PRIMARY KEY, json, src);
134 INSERT INTO j2(id,json,src)
141 "streetAddress": "21 2nd Street",
144 "postalCode": "10021-3100"
149 "number": "212 555-1234"
153 "number": "646 555-4567"
158 }','https://en.wikipedia.org/wiki/JSON');
159 INSERT INTO j2(id,json,src)
169 { "id": "1001", "type": "Regular" },
170 { "id": "1002", "type": "Chocolate" },
171 { "id": "1003", "type": "Blueberry" },
172 { "id": "1004", "type": "Devil''s Food" }
177 { "id": "5001", "type": "None" },
178 { "id": "5002", "type": "Glazed" },
179 { "id": "5005", "type": "Sugar" },
180 { "id": "5007", "type": "Powdered Sugar" },
181 { "id": "5006", "type": "Chocolate with Sprinkles" },
182 { "id": "5003", "type": "Chocolate" },
183 { "id": "5004", "type": "Maple" }
185 }','https://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html');
186 INSERT INTO j2(id,json,src)
197 { "id": "1001", "type": "Regular" },
198 { "id": "1002", "type": "Chocolate" },
199 { "id": "1003", "type": "Blueberry" },
200 { "id": "1004", "type": "Devil''s Food" }
205 { "id": "5001", "type": "None" },
206 { "id": "5002", "type": "Glazed" },
207 { "id": "5005", "type": "Sugar" },
208 { "id": "5007", "type": "Powdered Sugar" },
209 { "id": "5006", "type": "Chocolate with Sprinkles" },
210 { "id": "5003", "type": "Chocolate" },
211 { "id": "5004", "type": "Maple" }
223 { "id": "1001", "type": "Regular" }
228 { "id": "5001", "type": "None" },
229 { "id": "5002", "type": "Glazed" },
230 { "id": "5005", "type": "Sugar" },
231 { "id": "5003", "type": "Chocolate" },
232 { "id": "5004", "type": "Maple" }
238 "name": "Old Fashioned",
244 { "id": "1001", "type": "Regular" },
245 { "id": "1002", "type": "Chocolate" }
250 { "id": "5001", "type": "None" },
251 { "id": "5002", "type": "Glazed" },
252 { "id": "5003", "type": "Chocolate" },
253 { "id": "5004", "type": "Maple" }
256 ]','https://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html');
257 SELECT count(*) FROM j2;
260 do_execsql_test json101-5.2 {
261 SELECT id, json_valid(json), json_type(json), '|' FROM j2 ORDER BY id;
262 } {1 1 object | 2 1 object | 3 1 array |}
269 # fullkey is always the same as path+key (with appropriate formatting)
271 do_execsql_test json101-5.3 {
272 SELECT j2.rowid, jx.rowid, fullkey, path, key
273 FROM j2, json_tree(j2.json) AS jx
274 WHERE fullkey!=(path || CASE WHEN typeof(key)=='integer' THEN '['||key||']'
277 do_execsql_test json101-5.4 {
278 SELECT j2.rowid, jx.rowid, fullkey, path, key
279 FROM j2, json_each(j2.json) AS jx
280 WHERE fullkey!=(path || CASE WHEN typeof(key)=='integer' THEN '['||key||']'
285 # Verify that the json_each.json and json_tree.json output is always the
288 do_execsql_test json101-5.5 {
289 SELECT j2.rowid, jx.rowid, fullkey, path, key
290 FROM j2, json_each(j2.json) AS jx
291 WHERE jx.json<>j2.json;
293 do_execsql_test json101-5.6 {
294 SELECT j2.rowid, jx.rowid, fullkey, path, key
295 FROM j2, json_tree(j2.json) AS jx
296 WHERE jx.json<>j2.json;
298 do_execsql_test json101-5.7 {
299 SELECT j2.rowid, jx.rowid, fullkey, path, key
300 FROM j2, json_each(j2.json) AS jx
301 WHERE jx.value<>jx.atom AND type NOT IN ('array','object');
303 do_execsql_test json101-5.8 {
304 SELECT j2.rowid, jx.rowid, fullkey, path, key
305 FROM j2, json_tree(j2.json) AS jx
306 WHERE jx.value<>jx.atom AND type NOT IN ('array','object');
309 do_execsql_test json101-6.1 {
310 SELECT json_valid('{"a":55,"b":72,}');
312 do_execsql_test json101-6.2 {
313 SELECT json_error_position('{"a":55,"b":72,}');
315 do_execsql_test json101-6.3 {
316 SELECT json_valid(json('{"a":55,"b":72,}'));
318 do_execsql_test json101-6.4 {
319 SELECT json_valid('{"a":55,"b":72 , }');
321 do_execsql_test json101-6.5 {
322 SELECT json_error_position('{"a":55,"b":72 , }');
324 do_execsql_test json101-6.6 {
325 SELECT json_error_position('{"a":55,"b":72,,}');
327 do_execsql_test json101-6.7 {
328 SELECT json_valid('{"a":55,"b":72}');
330 do_execsql_test json101-6.8 {
331 SELECT json_error_position('["a",55,"b",72,]');
333 do_execsql_test json101-6.9 {
334 SELECT json_error_position('["a",55,"b",72 , ]');
336 do_execsql_test json101-6.10 {
337 SELECT json_error_position('["a",55,"b",72,,]');
339 do_execsql_test json101-6.11 {
340 SELECT json_valid('["a",55,"b",72]');
343 # White-space tests. Note that form-feed is not white-space in JSON.
344 # ticket [57eec374ae1d0a1d4a23077a95f4e173fe269113]
346 foreach {tn isvalid ws} {
352 7.6 1 char(0x20,0x09,0x0a,0x0d,0x20)
353 7.7 0 char(0x20,0x09,0x0a,0x0c,0x0d,0x20)
355 do_execsql_test json101-$tn.1 \
356 "SELECT json_valid(printf('%s{%s\"x\"%s:%s9%s}%s',
357 $::ws,$::ws,$::ws,$::ws,$::ws,$::ws));" \
361 # Ticket https://www.sqlite.org/src/info/ad2559db380abf8e
362 # Control characters must be escaped in JSON strings.
364 do_execsql_test json101-8.1 {
365 DROP TABLE IF EXISTS t8;
366 CREATE TABLE t8(a,b);
367 INSERT INTO t8(a) VALUES('abc' || char(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35) || 'xyz');
368 UPDATE t8 SET b=json_array(a);
370 } {{["abc\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\"#xyz"]}}
371 do_execsql_test json101-8.2 {
372 SELECT a=json_extract(b,'$[0]') FROM t8;
375 # 2017-04-12. Regression reported on the mailing list by Rolf Ade
377 do_execsql_test json101-8.3 {
378 SELECT json_valid(char(0x22,0xe4,0x22));
380 do_execsql_test json101-8.4 {
381 SELECT unicode(json_extract(char(0x22,228,0x22),'$'));
384 # The json_quote() function transforms an SQL value into a JSON value.
385 # String values are quoted and interior quotes are escaped. NULL values
386 # are rendered as the unquoted string "null".
388 do_execsql_test json101-9.1 {
389 SELECT json_quote('abc"xyz');
391 do_execsql_test json101-9.2 {
392 SELECT json_quote(3.14159);
394 do_execsql_test json101-9.3 {
395 SELECT json_quote(12345);
397 do_execsql_test json101-9.4 {
398 SELECT json_quote(null);
400 do_catchsql_test json101-9.5 {
401 SELECT json_quote(x'30313233');
402 } {1 {JSON cannot hold BLOB values}}
403 do_catchsql_test json101-9.6 {
404 SELECT json_quote(123,456)
405 } {1 {wrong number of arguments to function json_quote()}}
406 do_catchsql_test json101-9.7 {
408 } {1 {wrong number of arguments to function json_quote()}}
410 # Make sure only valid backslash-escapes are accepted.
412 do_execsql_test json101-10.1 {
413 SELECT json_valid('" \ "');
415 do_execsql_test json101-10.2 {
416 SELECT json_valid('" \! "');
418 do_execsql_test json101-10.3 {
419 SELECT json_valid('" \" "');
421 do_execsql_test json101-10.4 {
422 SELECT json_valid('" \# "');
424 do_execsql_test json101-10.5 {
425 SELECT json_valid('" \$ "');
427 do_execsql_test json101-10.6 {
428 SELECT json_valid('" \% "');
430 do_execsql_test json101-10.7 {
431 SELECT json_valid('" \& "');
433 do_execsql_test json101-10.8 {
434 SELECT json_valid('" \'' "');
436 do_execsql_test json101-10.9 {
437 SELECT json_valid('" \( "');
439 do_execsql_test json101-10.10 {
440 SELECT json_valid('" \) "');
442 do_execsql_test json101-10.11 {
443 SELECT json_valid('" \* "');
445 do_execsql_test json101-10.12 {
446 SELECT json_valid('" \+ "');
448 do_execsql_test json101-10.13 {
449 SELECT json_valid('" \, "');
451 do_execsql_test json101-10.14 {
452 SELECT json_valid('" \- "');
454 do_execsql_test json101-10.15 {
455 SELECT json_valid('" \. "');
457 do_execsql_test json101-10.16 {
458 SELECT json_valid('" \/ "');
460 do_execsql_test json101-10.17 {
461 SELECT json_valid('" \0 "');
463 do_execsql_test json101-10.18 {
464 SELECT json_valid('" \1 "');
466 do_execsql_test json101-10.19 {
467 SELECT json_valid('" \2 "');
469 do_execsql_test json101-10.20 {
470 SELECT json_valid('" \3 "');
472 do_execsql_test json101-10.21 {
473 SELECT json_valid('" \4 "');
475 do_execsql_test json101-10.22 {
476 SELECT json_valid('" \5 "');
478 do_execsql_test json101-10.23 {
479 SELECT json_valid('" \6 "');
481 do_execsql_test json101-10.24 {
482 SELECT json_valid('" \7 "');
484 do_execsql_test json101-10.25 {
485 SELECT json_valid('" \8 "');
487 do_execsql_test json101-10.26 {
488 SELECT json_valid('" \9 "');
490 do_execsql_test json101-10.27 {
491 SELECT json_valid('" \: "');
493 do_execsql_test json101-10.28 {
494 SELECT json_valid('" \; "');
496 do_execsql_test json101-10.29 {
497 SELECT json_valid('" \< "');
499 do_execsql_test json101-10.30 {
500 SELECT json_valid('" \= "');
502 do_execsql_test json101-10.31 {
503 SELECT json_valid('" \> "');
505 do_execsql_test json101-10.32 {
506 SELECT json_valid('" \? "');
508 do_execsql_test json101-10.33 {
509 SELECT json_valid('" \@ "');
511 do_execsql_test json101-10.34 {
512 SELECT json_valid('" \A "');
514 do_execsql_test json101-10.35 {
515 SELECT json_valid('" \B "');
517 do_execsql_test json101-10.36 {
518 SELECT json_valid('" \C "');
520 do_execsql_test json101-10.37 {
521 SELECT json_valid('" \D "');
523 do_execsql_test json101-10.38 {
524 SELECT json_valid('" \E "');
526 do_execsql_test json101-10.39 {
527 SELECT json_valid('" \F "');
529 do_execsql_test json101-10.40 {
530 SELECT json_valid('" \G "');
532 do_execsql_test json101-10.41 {
533 SELECT json_valid('" \H "');
535 do_execsql_test json101-10.42 {
536 SELECT json_valid('" \I "');
538 do_execsql_test json101-10.43 {
539 SELECT json_valid('" \J "');
541 do_execsql_test json101-10.44 {
542 SELECT json_valid('" \K "');
544 do_execsql_test json101-10.45 {
545 SELECT json_valid('" \L "');
547 do_execsql_test json101-10.46 {
548 SELECT json_valid('" \M "');
550 do_execsql_test json101-10.47 {
551 SELECT json_valid('" \N "');
553 do_execsql_test json101-10.48 {
554 SELECT json_valid('" \O "');
556 do_execsql_test json101-10.49 {
557 SELECT json_valid('" \P "');
559 do_execsql_test json101-10.50 {
560 SELECT json_valid('" \Q "');
562 do_execsql_test json101-10.51 {
563 SELECT json_valid('" \R "');
565 do_execsql_test json101-10.52 {
566 SELECT json_valid('" \S "');
568 do_execsql_test json101-10.53 {
569 SELECT json_valid('" \T "');
571 do_execsql_test json101-10.54 {
572 SELECT json_valid('" \U "');
574 do_execsql_test json101-10.55 {
575 SELECT json_valid('" \V "');
577 do_execsql_test json101-10.56 {
578 SELECT json_valid('" \W "');
580 do_execsql_test json101-10.57 {
581 SELECT json_valid('" \X "');
583 do_execsql_test json101-10.58 {
584 SELECT json_valid('" \Y "');
586 do_execsql_test json101-10.59 {
587 SELECT json_valid('" \Z "');
589 do_execsql_test json101-10.60 {
590 SELECT json_valid('" \[ "');
592 do_execsql_test json101-10.61 {
593 SELECT json_valid('" \\ "');
595 do_execsql_test json101-10.62 {
596 SELECT json_valid('" \] "');
598 do_execsql_test json101-10.63 {
599 SELECT json_valid('" \^ "');
601 do_execsql_test json101-10.64 {
602 SELECT json_valid('" \_ "');
604 do_execsql_test json101-10.65 {
605 SELECT json_valid('" \` "');
607 do_execsql_test json101-10.66 {
608 SELECT json_valid('" \a "');
610 do_execsql_test json101-10.67 {
611 SELECT json_valid('" \b "');
613 do_execsql_test json101-10.68 {
614 SELECT json_valid('" \c "');
616 do_execsql_test json101-10.69 {
617 SELECT json_valid('" \d "');
619 do_execsql_test json101-10.70 {
620 SELECT json_valid('" \e "');
622 do_execsql_test json101-10.71 {
623 SELECT json_valid('" \f "');
625 do_execsql_test json101-10.72 {
626 SELECT json_valid('" \g "');
628 do_execsql_test json101-10.73 {
629 SELECT json_valid('" \h "');
631 do_execsql_test json101-10.74 {
632 SELECT json_valid('" \i "');
634 do_execsql_test json101-10.75 {
635 SELECT json_valid('" \j "');
637 do_execsql_test json101-10.76 {
638 SELECT json_valid('" \k "');
640 do_execsql_test json101-10.77 {
641 SELECT json_valid('" \l "');
643 do_execsql_test json101-10.78 {
644 SELECT json_valid('" \m "');
646 do_execsql_test json101-10.79 {
647 SELECT json_valid('" \n "');
649 do_execsql_test json101-10.80 {
650 SELECT json_valid('" \o "');
652 do_execsql_test json101-10.81 {
653 SELECT json_valid('" \p "');
655 do_execsql_test json101-10.82 {
656 SELECT json_valid('" \q "');
658 do_execsql_test json101-10.83 {
659 SELECT json_valid('" \r "');
661 do_execsql_test json101-10.84 {
662 SELECT json_valid('" \s "');
664 do_execsql_test json101-10.85 {
665 SELECT json_valid('" \t "');
667 do_execsql_test json101-10.86.0 {
668 SELECT json_valid('" \u "');
670 do_execsql_test json101-10.86.1 {
671 SELECT json_valid('" \ua "');
673 do_execsql_test json101-10.86.2 {
674 SELECT json_valid('" \uab "');
676 do_execsql_test json101-10.86.3 {
677 SELECT json_valid('" \uabc "');
679 do_execsql_test json101-10.86.4 {
680 SELECT json_valid('" \uabcd "');
682 do_execsql_test json101-10.86.5 {
683 SELECT json_valid('" \uFEDC "');
685 do_execsql_test json101-10.86.6 {
686 SELECT json_valid('" \u1234 "');
688 do_execsql_test json101-10.87 {
689 SELECT json_valid('" \v "');
691 do_execsql_test json101-10.88 {
692 SELECT json_valid('" \w "');
694 do_execsql_test json101-10.89 {
695 SELECT json_valid('" \x "');
697 do_execsql_test json101-10.90 {
698 SELECT json_valid('" \y "');
700 do_execsql_test json101-10.91 {
701 SELECT json_valid('" \z "');
703 do_execsql_test json101-10.92 {
704 SELECT json_valid('" \{ "');
706 do_execsql_test json101-10.93 {
707 SELECT json_valid('" \| "');
709 do_execsql_test json101-10.94 {
710 SELECT json_valid('" \} "');
712 do_execsql_test json101-10.95 {
713 SELECT json_valid('" \~ "');
716 #--------------------------------------------------------------------------
717 # 2017-04-11. https://www.sqlite.org/src/info/981329adeef51011
718 # Stack overflow on deeply nested JSON.
720 # The following tests confirm that deeply nested JSON is considered invalid.
722 do_execsql_test json101-11.0 {
723 /* Shallow enough to be parsed */
724 SELECT json_valid(printf('%.1000c0%.1000c','[',']'));
726 do_execsql_test json101-11.1 {
727 /* Too deep by one */
728 SELECT json_valid(printf('%.1001c0%.1001c','[',']'));
730 do_execsql_test json101-11.2 {
731 /* Shallow enough to be parsed { */
732 SELECT json_valid(replace(printf('%.1000c0%.1000c','[','}'),'[','{"a":'));
735 do_execsql_test json101-11.3 {
736 /* Too deep by one { */
737 SELECT json_valid(replace(printf('%.1001c0%.1001c','[','}'),'[','{"a":'));
741 # 2017-10-27. Demonstrate the ability to access an element from
742 # a json structure even though the element name constains a "."
743 # character, by quoting the element name in the path.
745 do_execsql_test json101-12.100 {
747 INSERT INTO t12(x) VALUES(
751 {"forceDisplay":true,
752 "transliterate":true,
754 "summary.report":true},
756 {"forceDisplay":true,
757 "transliterate":false,
758 "add.footnote":false,
759 "summary.report":true},
761 {"forceDisplay":true,
762 "transliterate":false,
763 "add.footnote":false,
764 "summary.report":false}
769 do_execsql_test json101-12.110 {
770 SELECT json_remove(x, '$.settings.layer2."dis.legomenon".forceDisplay')
772 } {{{"settings":{"layer2":{"hapax.legomenon":{"forceDisplay":true,"transliterate":true,"add.footnote":true,"summary.report":true},"dis.legomenon":{"transliterate":false,"add.footnote":false,"summary.report":true},"tris.legomenon":{"forceDisplay":true,"transliterate":false,"add.footnote":false,"summary.report":false}}}}}}
773 do_execsql_test json101-12.120 {
774 SELECT json_extract(x, '$.settings.layer2."tris.legomenon"."summary.report"')
779 # ticket https://www.sqlite.org/src/tktview/80177f0c226ff54f6ddd41
780 # Make sure the query planner knows about the arguments to table-valued functions.
782 do_execsql_test json101-13.100 {
783 DROP TABLE IF EXISTS t1;
784 DROP TABLE IF EXISTS t2;
785 CREATE TABLE t1(id, json);
786 INSERT INTO t1(id,json) VALUES(1,'{"items":[3,5]}');
787 CREATE TABLE t2(id, json);
788 INSERT INTO t2(id,json) VALUES(2,'{"value":2}');
789 INSERT INTO t2(id,json) VALUES(3,'{"value":3}');
790 INSERT INTO t2(id,json) VALUES(4,'{"value":4}');
791 INSERT INTO t2(id,json) VALUES(5,'{"value":5}');
792 INSERT INTO t2(id,json) VALUES(6,'{"value":6}');
793 SELECT * FROM t1 CROSS JOIN t2
794 WHERE EXISTS(SELECT 1 FROM json_each(t1.json,'$.items') AS Z
795 WHERE Z.value==t2.id);
796 } {1 {{"items":[3,5]}} 3 {{"value":3}} 1 {{"items":[3,5]}} 5 {{"value":5}}}
797 do_execsql_test json101-13.110 {
798 SELECT * FROM t2 CROSS JOIN t1
799 WHERE EXISTS(SELECT 1 FROM json_each(t1.json,'$.items') AS Z
800 WHERE Z.value==t2.id);
801 } {3 {{"value":3}} 1 {{"items":[3,5]}} 5 {{"value":5}} 1 {{"items":[3,5]}}}
804 # Incorrect fullkey output from json_each()
805 # when the input JSON is not an array or object.
807 do_execsql_test json101-14.100 {
808 SELECT fullkey FROM json_each('123');
810 do_execsql_test json101-14.110 {
811 SELECT fullkey FROM json_each('123.56');
813 do_execsql_test json101-14.120 {
814 SELECT fullkey FROM json_each('"hello"');
816 do_execsql_test json101-14.130 {
817 SELECT fullkey FROM json_each('null');
819 do_execsql_test json101-14.140 {
820 SELECT fullkey FROM json_tree('123');
822 do_execsql_test json101-14.150 {
823 SELECT fullkey FROM json_tree('123.56');
825 do_execsql_test json101-14.160 {
826 SELECT fullkey FROM json_tree('"hello"');
828 do_execsql_test json101-14.170 {
829 SELECT fullkey FROM json_tree('null');
833 # Make sure the table-valued functions contained within parentheses
836 # Bug reported via private email. See TH3 for more information.
838 do_execsql_test json101-15.100 {
839 SELECT * FROM JSON_EACH('{"a":1, "b":2}');
840 } {a 1 integer 1 2 {} {$.a} {$} b 2 integer 2 4 {} {$.b} {$}}
841 do_execsql_test json101-15.110 {
842 SELECT xyz.* FROM JSON_EACH('{"a":1, "b":2}') AS xyz;
843 } {a 1 integer 1 2 {} {$.a} {$} b 2 integer 2 4 {} {$.b} {$}}
844 do_execsql_test json101-15.120 {
845 SELECT * FROM (JSON_EACH('{"a":1, "b":2}'));
846 } {a 1 integer 1 2 {} {$.a} {$} b 2 integer 2 4 {} {$.b} {$}}
847 do_execsql_test json101-15.130 {
848 SELECT xyz.* FROM (JSON_EACH('{"a":1, "b":2}')) AS xyz;
849 } {a 1 integer 1 2 {} {$.a} {$} b 2 integer 2 4 {} {$.b} {$}}
852 # Mailing list bug report on the handling of surrogate pairs
855 do_execsql_test json101-16.10 {
856 SELECT length(json_extract('"abc\uD834\uDD1Exyz"','$'));
858 do_execsql_test json101-16.20 {
859 SELECT length(json_extract('"\uD834\uDD1E"','$'));
861 do_execsql_test json101-16.30 {
862 SELECT unicode(json_extract('"\uD834\uDD1E"','$'));
865 # 2022-01-30 dbsqlfuzz 4678cf825d27f87c9b8343720121e12cf944b71a
866 do_execsql_test json101-17.1 {
867 DROP TABLE IF EXISTS t1;
868 DROP TABLE IF EXISTS t2;
869 CREATE TABLE t1(a,b,c);
871 SELECT * FROM t1 LEFT JOIN t2 ON (SELECT b FROM json_each ORDER BY 1);
874 # 2022-04-04 forum post https://sqlite.org/forum/forumpost/c082aeab43
875 do_execsql_test json101-18.1 {
876 SELECT json_valid('{"":5}');
878 do_execsql_test json101-18.2 {
879 SELECT json_extract('{"":5}', '$.""');
881 do_execsql_test json101-18.3 {
882 SELECT json_extract('[3,{"a":4,"":[5,{"hi":6},7]},8]', '$[1].""[1].hi');
884 do_execsql_test json101-18.4 {
885 SELECT json_extract('[3,{"a":4,"":[5,{"hi":6},7]},8]', '$[1].""[1]."hi"');
887 do_catchsql_test json101-18.5 {
888 SELECT json_extract('{"":8}', '$.');
889 } {1 {JSON path error near ''}}
891 # 2022-08-29 https://sqlite.org/forum/forumpost/9b9e4716c0d7bbd1
892 # This is not a problem specifically with JSON functions. It is
893 # a problem with transaction control. But the json() function makes
894 # the problem more easily accessible, so it is tested here.
896 do_execsql_test json101-19.1 {
897 DROP TABLE IF EXISTS t1;
900 do_catchsql_test json101-19.2 {
902 INSERT INTO t1 VALUES(0), (json('not-valid-json'));
903 } {1 {malformed JSON}}
904 do_execsql_test json101-19.3 {
909 # 2023-03-17 positive and negative infinities
911 do_execsql_test json101-20.1 {
912 SELECT json_object('a',2e370,'b',-3e380);
913 } {{{"a":9.0e+999,"b":-9.0e+999}}}
914 do_execsql_test json101-20.2 {
915 SELECT json_object('a',2e370,'b',-3e380)->>'a';
917 do_execsql_test json101-20.3 {
918 SELECT json_object('a',2e370,'b',-3e380)->>'b';
921 # 2023-05-02 https://sqlite.org/forum/forumpost/06c6334412
922 # JSON functions should normally return NULL when given
923 # a NULL value as the JSON input.
926 if {[db exists {SELECT * FROM pragma_compile_options WHERE compile_options LIKE '%legacy_json_valid%'}]} {
927 do_execsql_test json101-21.1-legacy {
928 SELECT json_valid(NULL);
931 do_execsql_test json101-21.1-correct {
932 SELECT json_valid(NULL);
935 do_execsql_test json101-21.2 {
936 SELECT json_error_position(NULL);
938 do_execsql_test json101-21.3 {
941 do_execsql_test json101-21.4 {
942 SELECT json_array(NULL);
944 do_execsql_test json101-21.5 {
945 SELECT json_extract(NULL);
947 do_execsql_test json101-21.6 {
948 SELECT json_insert(NULL,'$',123);
950 do_execsql_test json101-21.7 {
953 do_execsql_test json101-21.8 {
956 do_execsql_test json101-21.9 {
957 SELECT '{a:5}'->NULL;
959 do_execsql_test json101-21.10 {
960 SELECT '{a:5}'->>NULL;
962 do_catchsql_test json101-21.11 {
963 SELECT json_object(NULL,5);
964 } {1 {json_object() labels must be TEXT}}
965 do_execsql_test json101-21.12 {
966 SELECT json_patch(NULL,'{a:5}');
968 do_execsql_test json101-21.13 {
969 SELECT json_patch('{a:5}',NULL);
971 do_execsql_test json101-21.14 {
972 SELECT json_patch(NULL,NULL);
974 do_execsql_test json101-21.15 {
975 SELECT json_remove(NULL,'$');
977 do_execsql_test json101-21.16 {
978 SELECT json_remove('{a:5,b:7}',NULL);
980 do_execsql_test json101-21.17 {
981 SELECT json_replace(NULL,'$.a',123);
983 do_execsql_test json101-21.18 {
984 SELECT json_replace('{a:5,b:7}',NULL,NULL);
986 do_execsql_test json101-21.19 {
987 SELECT json_set(NULL,'$.a',123);
989 do_execsql_test json101-21.20 {
990 SELECT json_set('{a:5,b:7}',NULL,NULL);
992 do_execsql_test json101-21.21 {
993 SELECT json_type(NULL);
995 do_execsql_test json101-21.22 {
996 SELECT json_type('{a:5,b:7}',NULL);
998 do_execsql_test json101-21.23 {
999 SELECT json_quote(NULL);
1001 do_execsql_test json101-21.24 {
1002 SELECT count(*) FROM json_each(NULL);
1004 do_execsql_test json101-21.25 {
1005 SELECT count(*) FROM json_tree(NULL);
1007 do_execsql_test json101-21.26 {
1008 WITH c(x) AS (VALUES(1),(2.0),(NULL),('three'))
1009 SELECT json_group_array(x) FROM c;
1010 } {[1,2.0,null,"three"]}
1011 do_execsql_test json101-21.27 {
1012 WITH c(x,y) AS (VALUES('a',1),('b',2.0),('c',NULL),(NULL,'three'),('e','four'))
1013 SELECT json_group_object(x,y) FROM c;
1014 } {{{"a":1,"b":2.0,"c":null,:"three","e":"four"}}}