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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
|
# 2015-08-12
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements tests for JSON SQL functions extension to the
# SQLite library.
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
do_execsql_test json101-1.1.00 {
SELECT json_array(1,2.5,null,'hello');
} {[1,2.5,null,"hello"]}
do_execsql_test json101-1.1.01 {
SELECT json_array(1,'{"abc":2.5,"def":null,"ghi":hello}',99);
-- the second term goes in as a string:
} {[1,"{\\"abc\\":2.5,\\"def\\":null,\\"ghi\\":hello}",99]}
do_execsql_test json101-1.1.02 {
SELECT json_array(1,json('{"abc":2.5,"def":null,"ghi":"hello"}'),99);
-- the second term goes in as JSON
} {[1,{"abc":2.5,"def":null,"ghi":"hello"},99]}
do_execsql_test json101-1.1.03 {
SELECT json_array(1,json_object('abc',2.5,'def',null,'ghi','hello'),99);
-- the second term goes in as JSON
} {[1,{"abc":2.5,"def":null,"ghi":"hello"},99]}
do_execsql_test json101-1.2 {
SELECT hex(json_array('String "\ Test'));
} {5B22537472696E67205C225C5C2054657374225D}
do_catchsql_test json101-1.3 {
SELECT json_array(1,printf('%.1000c','x'),x'abcd',3);
} {1 {JSON cannot hold BLOB values}}
do_catchsql_test json101-1.3b {
SELECT jsonb_array(1,printf('%.1000c','x'),x'abcd',3);
} {1 {JSON cannot hold BLOB values}}
do_execsql_test json101-1.4 {
SELECT json_array(-9223372036854775808,9223372036854775807,0,1,-1,
0.0, 1.0, -1.0, -1e99, +2e100,
'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);
} {[-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]}
do_execsql_test json101-1.4b {
SELECT json(jsonb_array(-9223372036854775808,9223372036854775807,0,1,-1,
0.0, 1.0, -1.0, -1e99, +2e100,
'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));
} {[-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]}
do_execsql_test json101-2.1 {
SELECT json_object('a',1,'b',2.5,'c',null,'d','String Test');
} {{{"a":1,"b":2.5,"c":null,"d":"String Test"}}}
do_execsql_test json101-2.1b {
SELECT json(jsonb_object('a',1,'b',2.5,'c',null,'d','String Test'));
} {{{"a":1,"b":2.5,"c":null,"d":"String Test"}}}
do_catchsql_test json101-2.2 {
SELECT json_object('a',printf('%.1000c','x'),2,2.5);
} {1 {json_object() labels must be TEXT}}
do_catchsql_test json101-2.2b {
SELECT jsonb_object('a',printf('%.1000c','x'),2,2.5);
} {1 {json_object() labels must be TEXT}}
do_execsql_test json101-2.2.2 {
SELECT json_object('a',json_array('xyx',77,4.5),'x',2.5);
} {{{"a":["xyx",77,4.5],"x":2.5}}}
do_execsql_test json101-2.2.2b {
SELECT json(jsonb_object('a',json_array('xyx',77,4.5),'x',2.5));
} {{{"a":["xyx",77,4.5],"x":2.5}}}
do_execsql_test json101-2.2.3 {
SELECT json_object('a',jsonb_array('xyx',77,4.5),'x',2.5);
} {{{"a":["xyx",77,4.5],"x":2.5}}}
do_execsql_test json101-2.2.3b {
SELECT json(jsonb_object('a',jsonb_array('xyx',77,4.5),'x',2.5));
} {{{"a":["xyx",77,4.5],"x":2.5}}}
do_catchsql_test json101-2.3 {
SELECT json_object('a',1,'b');
} {1 {json_object() requires an even number of arguments}}
do_catchsql_test json101-2.4 {
SELECT json_object('a',printf('%.1000c','x'),'b',x'abcd');
} {1 {JSON cannot hold BLOB values}}
do_execsql_test json101-2.5 {
SELECT json_object('a',printf('%.10c','x'),'b',jsonb_array(1,2,3));
} {{{"a":"xxxxxxxxxx","b":[1,2,3]}}}
do_execsql_test json101-3.1 {
SELECT json_replace('{"a":1,"b":2}','$.a','[3,4,5]');
} {{{"a":"[3,4,5]","b":2}}}
do_execsql_test json101-3.1b {
SELECT json(jsonb_replace('{"a":1,"b":2}','$.a','[3,4,5]'));
} {{{"a":"[3,4,5]","b":2}}}
do_execsql_test json101-3.2 {
SELECT json_replace('{"a":1,"b":2}','$.a',json('[3,4,5]'));
} {{{"a":[3,4,5],"b":2}}}
do_execsql_test json101-3.2b {
SELECT json_replace('{"a":1,"b":2}','$.a',jsonb('[3,4,5]'));
} {{{"a":[3,4,5],"b":2}}}
do_execsql_test json101-3.3 {
SELECT json_type(json_set('{"a":1,"b":2}','$.b','{"x":3,"y":4}'),'$.b');
} {text}
do_execsql_test json101-3.3b {
SELECT json_type(jsonb_set('{"a":1,"b":2}','$.b','{"x":3,"y":4}'),'$.b');
} {text}
do_execsql_test json101-3.4 {
SELECT json_type(json_set('{"a":1,"b":2}','$.b',json('{"x":3,"y":4}')),'$.b');
} {object}
do_execsql_test json101-3.4b {
SELECT json_type(jsonb_set('{"a":1,"b":2}','$.b',jsonb('{"x":3,"y":4}')),'$.b');
} {object}
ifcapable vtab {
do_execsql_test json101-3.5 {
SELECT fullkey, atom, '|' FROM json_tree(json_set('{}','$.x',123,'$.x',456));
} {{$} {} | {$.x} 456 |}
do_execsql_test json101-3.5b {
SELECT fullkey, atom, '|' FROM json_tree(jsonb_set('{}','$.x',123,'$.x',456));
} {{$} {} | {$.x} 456 |}
}
# Per rfc7159, any JSON value is allowed at the top level, and whitespace
# is permitting before and/or after that value.
#
do_execsql_test json101-4.1 {
CREATE TABLE j1(x);
INSERT INTO j1(x)
VALUES('true'),('false'),('null'),('123'),('-234'),('34.5e+6'),
('""'),('"\""'),('"\\"'),('"abcdefghijlmnopqrstuvwxyz"'),
('[]'),('{}'),('[true,false,null,123,-234,34.5e+6,{},[]]'),
('{"a":true,"b":{"c":false}}');
SELECT * FROM j1 WHERE NOT json_valid(x);
} {}
do_execsql_test json101-4.2 {
SELECT * FROM j1 WHERE NOT json_valid(char(0x20,0x09,0x0a,0x0d)||x);
} {}
do_execsql_test json101-4.3 {
SELECT * FROM j1 WHERE NOT json_valid(x||char(0x20,0x09,0x0a,0x0d));
} {}
# But an empty string, or a string of pure whitespace is not valid JSON.
#
do_execsql_test json101-4.4 {
SELECT json_valid(''), json_valid(char(0x20,0x09,0x0a,0x0d));
} {0 0}
# json_remove() and similar functions with no edit operations return their
# input unchanged.
#
do_execsql_test json101-4.5 {
SELECT x FROM j1 WHERE json_remove(x)<>x;
} {}
do_execsql_test json101-4.6 {
SELECT x FROM j1 WHERE json_replace(x)<>x;
} {}
do_execsql_test json101-4.7 {
SELECT x FROM j1 WHERE json_set(x)<>x;
} {}
do_execsql_test json101-4.8 {
SELECT x FROM j1 WHERE json_insert(x)<>x;
} {}
do_execsql_test json101-4.9 {
SELECT json_insert('{"a":1}','$.b',CAST(x'0000' AS text));
} {{{"a":1,"b":"\u0000\u0000"}}}
# json_extract(JSON,'$') will return objects and arrays without change.
#
do_execsql_test json101-4.10 {
SELECT count(*) FROM j1 WHERE json_type(x) IN ('object','array');
SELECT x FROM j1
WHERE json_extract(x,'$')<>x
AND json_type(x) IN ('object','array');
} {4}
do_execsql_test json101-4.10b {
CREATE TABLE j1b AS SELECT jsonb(x) AS "x" FROM j1;
SELECT count(*) FROM j1b WHERE json_type(x) IN ('object','array');
SELECT json(x) FROM j1b
WHERE json_extract(x,'$')<>json(x)
AND json_type(x) IN ('object','array');
} {4}
do_execsql_test json101-5.1 {
CREATE TABLE j2(id INTEGER PRIMARY KEY, json, src);
INSERT INTO j2(id,json,src)
VALUES(1,'{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [],
"spouse": null
}','https://en.wikipedia.org/wiki/JSON');
INSERT INTO j2(id,json,src)
VALUES(2, '{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil''s Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
}','https://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html');
INSERT INTO j2(id,json,src)
VALUES(3,'[
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil''s Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0002",
"type": "donut",
"name": "Raised",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0003",
"type": "donut",
"name": "Old Fashioned",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
}
]','https://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html');
SELECT count(*) FROM j2;
CREATE TABLE j2b(id INTEGER PRIMARY KEY, json, src);
INSERT INTO J2b(id,json,src) SELECT id, jsonb(json), src FROM j2;
SELECT count(*) FROM j2b;
} {3 3}
do_execsql_test json101-5.2 {
SELECT id, json_valid(json), json_type(json), '|' FROM j2 ORDER BY id;
} {1 1 object | 2 1 object | 3 1 array |}
do_execsql_test json101-5.2b {
SELECT id, json_valid(json,5), json_type(json), '|' FROM j2b ORDER BY id;
} {1 1 object | 2 1 object | 3 1 array |}
ifcapable !vtab {
finish_test
return
}
# fullkey is always the same as path+key (with appropriate formatting)
#
do_execsql_test json101-5.3 {
SELECT j2.rowid, jx.rowid, fullkey, path, key
FROM j2, json_tree(j2.json) AS jx
WHERE fullkey!=(path || CASE WHEN typeof(key)=='integer' THEN '['||key||']'
ELSE '.'||key END);
} {}
do_execsql_test json101-5.3b {
SELECT j2b.rowid, jx.rowid, fullkey, path, key
FROM j2b, json_tree(j2b.json) AS jx
WHERE fullkey!=(path || CASE WHEN typeof(key)=='integer' THEN '['||key||']'
ELSE '.'||key END);
} {}
do_execsql_test json101-5.4 {
SELECT j2.rowid, jx.rowid, fullkey, path, key
FROM j2, json_each(j2.json) AS jx
WHERE fullkey!=(path || CASE WHEN typeof(key)=='integer' THEN '['||key||']'
ELSE '.'||key END);
} {}
# Verify that the json_each.json and json_tree.json output is always the
# same as input.
#
do_execsql_test json101-5.5 {
SELECT j2.rowid, jx.rowid, fullkey, path, key
FROM j2, json_each(j2.json) AS jx
WHERE jx.json<>j2.json;
} {}
do_execsql_test json101-5.6 {
SELECT j2.rowid, jx.rowid, fullkey, path, key
FROM j2, json_tree(j2.json) AS jx
WHERE jx.json<>j2.json;
} {}
do_execsql_test json101-5.7 {
SELECT j2.rowid, jx.rowid, fullkey, path, key
FROM j2, json_each(j2.json) AS jx
WHERE jx.value<>jx.atom AND type NOT IN ('array','object');
} {}
do_execsql_test json101-5.8 {
SELECT j2.rowid, jx.rowid, fullkey, path, key
FROM j2, json_tree(j2.json) AS jx
WHERE jx.value<>jx.atom AND type NOT IN ('array','object');
} {}
# 2024-02-16 https://sqlite.org/forum/forumpost/ecb94cd210
# Regression in json_tree()/json_each(). The value column
# should have the "J" subtype if the value is an array or
# object.
#
do_execsql_test json101-5.10 {
SELECT json_insert('{}','$.a',value) FROM json_tree('[1,2,3]') WHERE atom IS NULL;
} {{{"a":[1,2,3]}}}
# ^^^^^^^--- In double-quotes, a string literal, prior to bug fix
do_execsql_test json101-5.11 {
SELECT json_insert('{}','$.a',value) FROM json_tree('"[1,2,3]"');
} {{{"a":"[1,2,3]"}}}
do_execsql_test json101-6.1 {
SELECT json_valid('{"a":55,"b":72,}');
} {0}
do_execsql_test json101-6.2 {
SELECT json_error_position('{"a":55,"b":72,}');
} {0}
do_execsql_test json101-6.3 {
SELECT json_valid(json('{"a":55,"b":72,}'));
} {1}
do_execsql_test json101-6.4 {
SELECT json_valid('{"a":55,"b":72 , }');
} {0}
do_execsql_test json101-6.5 {
SELECT json_error_position('{"a":55,"b":72 , }');
} {0}
do_execsql_test json101-6.6 {
SELECT json_error_position('{"a":55,"b":72,,}');
} {16}
do_execsql_test json101-6.7 {
SELECT json_valid('{"a":55,"b":72}');
} {1}
do_execsql_test json101-6.8 {
SELECT json_error_position('["a",55,"b",72,]');
} {0}
do_execsql_test json101-6.9 {
SELECT json_error_position('["a",55,"b",72 , ]');
} {0}
do_execsql_test json101-6.10 {
SELECT json_error_position('["a",55,"b",72,,]');
} {16}
do_execsql_test json101-6.11 {
SELECT json_valid('["a",55,"b",72]');
} {1}
# White-space tests. Note that form-feed is not white-space in JSON.
# ticket [57eec374ae1d0a1d4a23077a95f4e173fe269113]
#
foreach {tn isvalid ws} {
7.1 1 char(0x20)
7.2 1 char(0x09)
7.3 1 char(0x0A)
7.4 1 char(0x0D)
7.5 0 char(0x0C)
7.6 1 char(0x20,0x09,0x0a,0x0d,0x20)
7.7 0 char(0x20,0x09,0x0a,0x0c,0x0d,0x20)
} {
do_execsql_test json101-$tn.1 \
"SELECT json_valid(printf('%s{%s\"x\"%s:%s9%s}%s',
$::ws,$::ws,$::ws,$::ws,$::ws,$::ws));" \
$isvalid
}
# Ticket https://www.sqlite.org/src/info/ad2559db380abf8e
# Control characters must be escaped in JSON strings.
#
do_execsql_test json101-8.1 {
DROP TABLE IF EXISTS t8;
CREATE TABLE t8(a,b);
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');
UPDATE t8 SET b=json_array(a);
SELECT b FROM t8;
} {{["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"]}}
do_execsql_test json101-8.1b {
DROP TABLE IF EXISTS t8;
CREATE TABLE t8(a,b);
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');
UPDATE t8 SET b=jsonb_array(a);
SELECT json(b) FROM t8;
} {{["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"]}}
do_execsql_test json101-8.2 {
SELECT a=json_extract(b,'$[0]') FROM t8;
} {1}
# 2017-04-12. Regression reported on the mailing list by Rolf Ade
#
do_execsql_test json101-8.3 {
SELECT json_valid(char(0x22,0xe4,0x22));
} {1}
do_execsql_test json101-8.4 {
SELECT unicode(json_extract(char(0x22,228,0x22),'$'));
} {228}
# The json_quote() function transforms an SQL value into a JSON value.
# String values are quoted and interior quotes are escaped. NULL values
# are rendered as the unquoted string "null".
#
do_execsql_test json101-9.1 {
SELECT json_quote('abc"xyz');
} {{"abc\"xyz"}}
do_execsql_test json101-9.2 {
SELECT json_quote(3.14159);
} {3.14159}
do_execsql_test json101-9.3 {
SELECT json_quote(12345);
} {12345}
do_execsql_test json101-9.4 {
SELECT json_quote(null);
} {"null"}
do_catchsql_test json101-9.5 {
SELECT json_quote(x'3031323334');
} {1 {JSON cannot hold BLOB values}}
do_catchsql_test json101-9.6 {
SELECT json_quote(123,456)
} {1 {wrong number of arguments to function json_quote()}}
do_catchsql_test json101-9.7 {
SELECT json_quote()
} {1 {wrong number of arguments to function json_quote()}}
# Make sure only valid backslash-escapes are accepted.
#
do_execsql_test json101-10.1 {
SELECT json_valid('" \ "');
} {0}
do_execsql_test json101-10.2 {
SELECT json_valid('" \! "');
} {0}
do_execsql_test json101-10.3 {
SELECT json_valid('" \" "');
} {1}
do_execsql_test json101-10.4 {
SELECT json_valid('" \# "');
} {0}
do_execsql_test json101-10.5 {
SELECT json_valid('" \$ "');
} {0}
do_execsql_test json101-10.6 {
SELECT json_valid('" \% "');
} {0}
do_execsql_test json101-10.7 {
SELECT json_valid('" \& "');
} {0}
do_execsql_test json101-10.8 {
SELECT json_valid('" \'' "');
} {0}
do_execsql_test json101-10.9 {
SELECT json_valid('" \( "');
} {0}
do_execsql_test json101-10.10 {
SELECT json_valid('" \) "');
} {0}
do_execsql_test json101-10.11 {
SELECT json_valid('" \* "');
} {0}
do_execsql_test json101-10.12 {
SELECT json_valid('" \+ "');
} {0}
do_execsql_test json101-10.13 {
SELECT json_valid('" \, "');
} {0}
do_execsql_test json101-10.14 {
SELECT json_valid('" \- "');
} {0}
do_execsql_test json101-10.15 {
SELECT json_valid('" \. "');
} {0}
do_execsql_test json101-10.16 {
SELECT json_valid('" \/ "');
} {1}
do_execsql_test json101-10.17 {
SELECT json_valid('" \0 "');
} {0}
do_execsql_test json101-10.18 {
SELECT json_valid('" \1 "');
} {0}
do_execsql_test json101-10.19 {
SELECT json_valid('" \2 "');
} {0}
do_execsql_test json101-10.20 {
SELECT json_valid('" \3 "');
} {0}
do_execsql_test json101-10.21 {
SELECT json_valid('" \4 "');
} {0}
do_execsql_test json101-10.22 {
SELECT json_valid('" \5 "');
} {0}
do_execsql_test json101-10.23 {
SELECT json_valid('" \6 "');
} {0}
do_execsql_test json101-10.24 {
SELECT json_valid('" \7 "');
} {0}
do_execsql_test json101-10.25 {
SELECT json_valid('" \8 "');
} {0}
do_execsql_test json101-10.26 {
SELECT json_valid('" \9 "');
} {0}
do_execsql_test json101-10.27 {
SELECT json_valid('" \: "');
} {0}
do_execsql_test json101-10.28 {
SELECT json_valid('" \; "');
} {0}
do_execsql_test json101-10.29 {
SELECT json_valid('" \< "');
} {0}
do_execsql_test json101-10.30 {
SELECT json_valid('" \= "');
} {0}
do_execsql_test json101-10.31 {
SELECT json_valid('" \> "');
} {0}
do_execsql_test json101-10.32 {
SELECT json_valid('" \? "');
} {0}
do_execsql_test json101-10.33 {
SELECT json_valid('" \@ "');
} {0}
do_execsql_test json101-10.34 {
SELECT json_valid('" \A "');
} {0}
do_execsql_test json101-10.35 {
SELECT json_valid('" \B "');
} {0}
do_execsql_test json101-10.36 {
SELECT json_valid('" \C "');
} {0}
do_execsql_test json101-10.37 {
SELECT json_valid('" \D "');
} {0}
do_execsql_test json101-10.38 {
SELECT json_valid('" \E "');
} {0}
do_execsql_test json101-10.39 {
SELECT json_valid('" \F "');
} {0}
do_execsql_test json101-10.40 {
SELECT json_valid('" \G "');
} {0}
do_execsql_test json101-10.41 {
SELECT json_valid('" \H "');
} {0}
do_execsql_test json101-10.42 {
SELECT json_valid('" \I "');
} {0}
do_execsql_test json101-10.43 {
SELECT json_valid('" \J "');
} {0}
do_execsql_test json101-10.44 {
SELECT json_valid('" \K "');
} {0}
do_execsql_test json101-10.45 {
SELECT json_valid('" \L "');
} {0}
do_execsql_test json101-10.46 {
SELECT json_valid('" \M "');
} {0}
do_execsql_test json101-10.47 {
SELECT json_valid('" \N "');
} {0}
do_execsql_test json101-10.48 {
SELECT json_valid('" \O "');
} {0}
do_execsql_test json101-10.49 {
SELECT json_valid('" \P "');
} {0}
do_execsql_test json101-10.50 {
SELECT json_valid('" \Q "');
} {0}
do_execsql_test json101-10.51 {
SELECT json_valid('" \R "');
} {0}
do_execsql_test json101-10.52 {
SELECT json_valid('" \S "');
} {0}
do_execsql_test json101-10.53 {
SELECT json_valid('" \T "');
} {0}
do_execsql_test json101-10.54 {
SELECT json_valid('" \U "');
} {0}
do_execsql_test json101-10.55 {
SELECT json_valid('" \V "');
} {0}
do_execsql_test json101-10.56 {
SELECT json_valid('" \W "');
} {0}
do_execsql_test json101-10.57 {
SELECT json_valid('" \X "');
} {0}
do_execsql_test json101-10.58 {
SELECT json_valid('" \Y "');
} {0}
do_execsql_test json101-10.59 {
SELECT json_valid('" \Z "');
} {0}
do_execsql_test json101-10.60 {
SELECT json_valid('" \[ "');
} {0}
do_execsql_test json101-10.61 {
SELECT json_valid('" \\ "');
} {1}
do_execsql_test json101-10.62 {
SELECT json_valid('" \] "');
} {0}
do_execsql_test json101-10.63 {
SELECT json_valid('" \^ "');
} {0}
do_execsql_test json101-10.64 {
SELECT json_valid('" \_ "');
} {0}
do_execsql_test json101-10.65 {
SELECT json_valid('" \` "');
} {0}
do_execsql_test json101-10.66 {
SELECT json_valid('" \a "');
} {0}
do_execsql_test json101-10.67 {
SELECT json_valid('" \b "');
} {1}
do_execsql_test json101-10.68 {
SELECT json_valid('" \c "');
} {0}
do_execsql_test json101-10.69 {
SELECT json_valid('" \d "');
} {0}
do_execsql_test json101-10.70 {
SELECT json_valid('" \e "');
} {0}
do_execsql_test json101-10.71 {
SELECT json_valid('" \f "');
} {1}
do_execsql_test json101-10.72 {
SELECT json_valid('" \g "');
} {0}
do_execsql_test json101-10.73 {
SELECT json_valid('" \h "');
} {0}
do_execsql_test json101-10.74 {
SELECT json_valid('" \i "');
} {0}
do_execsql_test json101-10.75 {
SELECT json_valid('" \j "');
} {0}
do_execsql_test json101-10.76 {
SELECT json_valid('" \k "');
} {0}
do_execsql_test json101-10.77 {
SELECT json_valid('" \l "');
} {0}
do_execsql_test json101-10.78 {
SELECT json_valid('" \m "');
} {0}
do_execsql_test json101-10.79 {
SELECT json_valid('" \n "');
} {1}
do_execsql_test json101-10.80 {
SELECT json_valid('" \o "');
} {0}
do_execsql_test json101-10.81 {
SELECT json_valid('" \p "');
} {0}
do_execsql_test json101-10.82 {
SELECT json_valid('" \q "');
} {0}
do_execsql_test json101-10.83 {
SELECT json_valid('" \r "');
} {1}
do_execsql_test json101-10.84 {
SELECT json_valid('" \s "');
} {0}
do_execsql_test json101-10.85 {
SELECT json_valid('" \t "');
} {1}
do_execsql_test json101-10.86.0 {
SELECT json_valid('" \u "');
} {0}
do_execsql_test json101-10.86.1 {
SELECT json_valid('" \ua "');
} {0}
do_execsql_test json101-10.86.2 {
SELECT json_valid('" \uab "');
} {0}
do_execsql_test json101-10.86.3 {
SELECT json_valid('" \uabc "');
} {0}
do_execsql_test json101-10.86.4 {
SELECT json_valid('" \uabcd "');
} {1}
do_execsql_test json101-10.86.5 {
SELECT json_valid('" \uFEDC "');
} {1}
do_execsql_test json101-10.86.6 {
SELECT json_valid('" \u1234 "');
} {1}
do_execsql_test json101-10.87 {
SELECT json_valid('" \v "');
} {0}
do_execsql_test json101-10.88 {
SELECT json_valid('" \w "');
} {0}
do_execsql_test json101-10.89 {
SELECT json_valid('" \x "');
} {0}
do_execsql_test json101-10.90 {
SELECT json_valid('" \y "');
} {0}
do_execsql_test json101-10.91 {
SELECT json_valid('" \z "');
} {0}
do_execsql_test json101-10.92 {
SELECT json_valid('" \{ "');
} {0}
do_execsql_test json101-10.93 {
SELECT json_valid('" \| "');
} {0}
do_execsql_test json101-10.94 {
SELECT json_valid('" \} "');
} {0}
do_execsql_test json101-10.95 {
SELECT json_valid('" \~ "');
} {0}
#--------------------------------------------------------------------------
# 2017-04-11. https://www.sqlite.org/src/info/981329adeef51011
# Stack overflow on deeply nested JSON.
#
# The following tests confirm that deeply nested JSON is considered invalid.
#
do_execsql_test json101-11.0 {
/* Shallow enough to be parsed */
SELECT json_valid(printf('%.1000c0%.1000c','[',']'));
} {1}
do_execsql_test json101-11.1 {
/* Too deep by one */
SELECT json_valid(printf('%.1001c0%.1001c','[',']'));
} {0}
do_execsql_test json101-11.2 {
/* Shallow enough to be parsed { */
SELECT json_valid(replace(printf('%.1000c0%.1000c','[','}'),'[','{"a":'));
/* } */
} {1}
do_execsql_test json101-11.3 {
/* Too deep by one { */
SELECT json_valid(replace(printf('%.1001c0%.1001c','[','}'),'[','{"a":'));
/* } */
} {0}
# 2017-10-27. Demonstrate the ability to access an element from
# a json structure even though the element name constains a "."
# character, by quoting the element name in the path.
#
do_execsql_test json101-12.100 {
CREATE TABLE t12(x);
INSERT INTO t12(x) VALUES(
'{"settings":
{"layer2":
{"hapax.legomenon":
{"forceDisplay":true,
"transliterate":true,
"add.footnote":true,
"summary.report":true},
"dis.legomenon":
{"forceDisplay":true,
"transliterate":false,
"add.footnote":false,
"summary.report":true},
"tris.legomenon":
{"forceDisplay":true,
"transliterate":false,
"add.footnote":false,
"summary.report":false}
}
}
}');
} {}
do_execsql_test json101-12.110 {
SELECT json_remove(x, '$.settings.layer2."dis.legomenon".forceDisplay')
FROM t12;
} {{{"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}}}}}}
do_execsql_test json101-12.110b {
SELECT json_remove(jsonb(x), '$.settings.layer2."dis.legomenon".forceDisplay')
FROM t12;
} {{{"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}}}}}}
do_execsql_test json101-12.120 {
SELECT json_extract(x, '$.settings.layer2."tris.legomenon"."summary.report"')
FROM t12;
} {0}
do_execsql_test json101-12.120b {
SELECT json_extract(jsonb(x), '$.settings.layer2."tris.legomenon"."summary.report"')
FROM t12;
} {0}
# 2018-01-26
# ticket https://www.sqlite.org/src/tktview/80177f0c226ff54f6ddd41
# Make sure the query planner knows about the arguments to table-valued functions.
#
do_execsql_test json101-13.100 {
DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t2;
CREATE TABLE t1(id, json);
INSERT INTO t1(id,json) VALUES(1,'{"items":[3,5]}');
CREATE TABLE t2(id, json);
INSERT INTO t2(id,json) VALUES(2,'{"value":2}');
INSERT INTO t2(id,json) VALUES(3,'{"value":3}');
INSERT INTO t2(id,json) VALUES(4,'{"value":4}');
INSERT INTO t2(id,json) VALUES(5,'{"value":5}');
INSERT INTO t2(id,json) VALUES(6,'{"value":6}');
SELECT * FROM t1 CROSS JOIN t2
WHERE EXISTS(SELECT 1 FROM json_each(t1.json,'$.items') AS Z
WHERE Z.value==t2.id);
} {1 {{"items":[3,5]}} 3 {{"value":3}} 1 {{"items":[3,5]}} 5 {{"value":5}}}
do_execsql_test json101-13.110 {
SELECT * FROM t2 CROSS JOIN t1
WHERE EXISTS(SELECT 1 FROM json_each(t1.json,'$.items') AS Z
WHERE Z.value==t2.id);
} {3 {{"value":3}} 1 {{"items":[3,5]}} 5 {{"value":5}} 1 {{"items":[3,5]}}}
# 2018-05-16
# Incorrect fullkey output from json_each()
# when the input JSON is not an array or object.
#
do_execsql_test json101-14.100 {
SELECT fullkey FROM json_each('123');
} {$}
do_execsql_test json101-14.110 {
SELECT fullkey FROM json_each('123.56');
} {$}
do_execsql_test json101-14.120 {
SELECT fullkey FROM json_each('"hello"');
} {$}
do_execsql_test json101-14.130 {
SELECT fullkey FROM json_each('null');
} {$}
do_execsql_test json101-14.140 {
SELECT fullkey FROM json_tree('123');
} {$}
do_execsql_test json101-14.150 {
SELECT fullkey FROM json_tree('123.56');
} {$}
do_execsql_test json101-14.160 {
SELECT fullkey FROM json_tree('"hello"');
} {$}
do_execsql_test json101-14.170 {
SELECT fullkey FROM json_tree('null');
} {$}
# 2018-12-03
# Make sure the table-valued functions contained within parentheses
# work correctly.
#
# Bug reported via private email. See TH3 for more information.
#
do_execsql_test json101-15.100 {
SELECT * FROM JSON_EACH('{"a":1, "b":2}');
} {a 1 integer 1 1 {} {$.a} {$} b 2 integer 2 5 {} {$.b} {$}}
do_execsql_test json101-15.110 {
SELECT xyz.* FROM JSON_EACH('{"a":1, "b":2}') AS xyz;
} {a 1 integer 1 1 {} {$.a} {$} b 2 integer 2 5 {} {$.b} {$}}
do_execsql_test json101-15.120 {
SELECT * FROM (JSON_EACH('{"a":1, "b":2}'));
} {a 1 integer 1 1 {} {$.a} {$} b 2 integer 2 5 {} {$.b} {$}}
do_execsql_test json101-15.130 {
SELECT xyz.* FROM (JSON_EACH('{"a":1, "b":2}')) AS xyz;
} {a 1 integer 1 1 {} {$.a} {$} b 2 integer 2 5 {} {$.b} {$}}
# 2019-11-10
# Mailing list bug report on the handling of surrogate pairs
# in JSON.
#
do_execsql_test json101-16.10 {
SELECT length(json_extract('"abc\uD834\uDD1Exyz"','$'));
} {7}
do_execsql_test json101-16.20 {
SELECT length(json_extract('"\uD834\uDD1E"','$'));
} {1}
do_execsql_test json101-16.30 {
SELECT unicode(json_extract('"\uD834\uDD1E"','$'));
} {119070}
# 2022-01-30 dbsqlfuzz 4678cf825d27f87c9b8343720121e12cf944b71a
do_execsql_test json101-17.1 {
DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t2;
CREATE TABLE t1(a,b,c);
CREATE TABLE t2(d);
SELECT * FROM t1 LEFT JOIN t2 ON (SELECT b FROM json_each ORDER BY 1);
} {}
# 2022-04-04 forum post https://sqlite.org/forum/forumpost/c082aeab43
do_execsql_test json101-18.1 {
SELECT json_valid('{"":5}');
} {1}
do_execsql_test json101-18.2 {
SELECT json_extract('{"":5}', '$.""');
} {5}
do_execsql_test json101-18.3 {
SELECT json_extract('[3,{"a":4,"":[5,{"hi":6},7]},8]', '$[1].""[1].hi');
} {6}
do_execsql_test json101-18.4 {
SELECT json_extract('[3,{"a":4,"":[5,{"hi":6},7]},8]', '$[1].""[1]."hi"');
} {6}
do_catchsql_test json101-18.5 {
SELECT json_extract('{"":8}', '$.');
} {1 {bad JSON path: '$.'}}
# 2022-08-29 https://sqlite.org/forum/forumpost/9b9e4716c0d7bbd1
# This is not a problem specifically with JSON functions. It is
# a problem with transaction control. But the json() function makes
# the problem more easily accessible, so it is tested here.
#
do_execsql_test json101-19.1 {
DROP TABLE IF EXISTS t1;
CREATE TABLE t1(x);
} {}
do_catchsql_test json101-19.2 {
BEGIN;
INSERT INTO t1 VALUES(0), (json('not-valid-json'));
} {1 {malformed JSON}}
do_execsql_test json101-19.3 {
COMMIT;
SELECT * FROM t1;
} {}
# 2023-03-17 positive and negative infinities
#
do_execsql_test json101-20.1 {
SELECT json_object('a',2e370,'b',-3e380);
} {{{"a":9.0e+999,"b":-9.0e+999}}}
do_execsql_test json101-20.2 {
SELECT json_object('a',2e370,'b',-3e380)->>'a';
} Inf
do_execsql_test json101-20.3 {
SELECT json_object('a',2e370,'b',-3e380)->>'b';
} {-Inf}
# 2023-05-02 https://sqlite.org/forum/forumpost/06c6334412
# JSON functions should normally return NULL when given
# a NULL value as the JSON input.
#
db null NULL
if {[db exists {SELECT * FROM pragma_compile_options WHERE compile_options LIKE '%legacy_json_valid%'}]} {
do_execsql_test json101-21.1-legacy {
SELECT json_valid(NULL);
} 0
} else {
do_execsql_test json101-21.1-correct {
SELECT json_valid(NULL);
} NULL
}
do_execsql_test json101-21.2 {
SELECT json_error_position(NULL);
} NULL
do_execsql_test json101-21.3 {
SELECT json(NULL);
} NULL
do_execsql_test json101-21.4 {
SELECT json_array(NULL);
} {[null]}
do_execsql_test json101-21.5 {
SELECT json_extract(NULL);
} NULL
do_execsql_test json101-21.6 {
SELECT json_insert(NULL,'$',123);
} NULL
do_execsql_test json101-21.7 {
SELECT NULL->0;
} NULL
do_execsql_test json101-21.8 {
SELECT NULL->>0;
} NULL
do_execsql_test json101-21.9 {
SELECT '{a:5}'->NULL;
} NULL
do_execsql_test json101-21.10 {
SELECT '{a:5}'->>NULL;
} NULL
do_catchsql_test json101-21.11 {
SELECT json_object(NULL,5);
} {1 {json_object() labels must be TEXT}}
do_execsql_test json101-21.12 {
SELECT json_patch(NULL,'{a:5}');
} NULL
do_execsql_test json101-21.13 {
SELECT json_patch('{a:5}',NULL);
} NULL
do_execsql_test json101-21.14 {
SELECT json_patch(NULL,NULL);
} NULL
do_execsql_test json101-21.15 {
SELECT json_remove(NULL,'$');
} NULL
do_execsql_test json101-21.16 {
SELECT json_remove('{a:5,b:7}',NULL);
} NULL
do_execsql_test json101-21.17 {
SELECT json_replace(NULL,'$.a',123);
} NULL
do_execsql_test json101-21.18 {
SELECT json_replace('{a:5,b:7}',NULL,NULL);
} {{{"a":5,"b":7}}}
do_execsql_test json101-21.19 {
SELECT json_set(NULL,'$.a',123);
} NULL
do_execsql_test json101-21.20 {
SELECT json_set('{a:5,b:7}',NULL,NULL);
} {{{"a":5,"b":7}}}
do_execsql_test json101-21.21 {
SELECT json_type(NULL);
} NULL
do_execsql_test json101-21.22 {
SELECT json_type('{a:5,b:7}',NULL);
} NULL
do_execsql_test json101-21.23 {
SELECT json_quote(NULL);
} null
do_execsql_test json101-21.24 {
SELECT count(*) FROM json_each(NULL);
} 0
do_execsql_test json101-21.25 {
SELECT count(*) FROM json_tree(NULL);
} 0
do_execsql_test json101-21.26 {
WITH c(x) AS (VALUES(1),(2.0),(NULL),('three'))
SELECT json_group_array(x) FROM c;
} {[1,2.0,null,"three"]}
do_execsql_test json101-21.27 {
WITH c(x,y) AS (VALUES('a',1),('b',2.0),('c',NULL),(NULL,'three'),('e','four'))
SELECT json_group_object(x,y) FROM c;
} {{{"a":1,"b":2.0,"c":null,:"three","e":"four"}}}
# 2023-10-09 https://sqlite.org/forum/forumpost/b25edc1d46
# UAF due to JSON cache overflow
#
do_execsql_test json101-22.1 {
SELECT json_set(
'{}',
'$.a', json('1'),
'$.a', json('2'),
'$.b', json('3'),
'$.b', json('4'),
'$.c', json('5'),
'$.c', json('6')
);
} {{{"a":2,"b":4,"c":6}}}
do_execsql_test json101-22.2 {
SELECT json_replace(
'{"a":7,"b":8,"c":9}',
'$.a', json('1'),
'$.a', json('2'),
'$.b', json('3'),
'$.b', json('4'),
'$.c', json('5'),
'$.c', json('6')
);
} {{{"a":2,"b":4,"c":6}}}
# 2023-10-17 https://sqlite.org/forum/forumpost/fc0e3f1e2a
# Incorrect accesss to '$[0]' in parsed + edited JSON.
#
do_execsql_test json101-23.1 {
SELECT j, j->>0, j->>1
FROM (SELECT json_set(json_set('[]','$[#]',0), '$[#]',1) AS j);
} {{[0,1]} 0 1}
do_execsql_test json101-23.2 {
SELECT j, j->>0, j->>1
FROM (SELECT json_set('[]','$[#]',0,'$[#]',1) AS j);
} {{[0,1]} 0 1}
# Insert/Set/Replace where the path specifies substructure that
# does not yet exist
#
proc tx x {return [string map [list ( \173 ) \175 ' \042 < \133 > \135] $x]}
foreach {id start path ins set repl} {
1 {{}} {$.a.b.c} ('a':('b':('c':9))) ('a':('b':('c':9))) ()
2 {{a:4}} {$.a.b.c} ('a':4) ('a':4) ('a':4)
3 {{a:{}}} {$.a.b.c} ('a':('b':('c':9))) ('a':('b':('c':9))) ('a':())
4 {[0,1,2]} {$[3].a[0].b} <0,1,2,('a':<('b':9)>)> <0,1,2,('a':<('b':9)>)> <0,1,2>
5 {[0,1,2]} {$[1].a[0].b} <0,1,2> <0,1,2> <0,1,2>
6 {[0,{},2]} {$[1].a[0].b} <0,('a':<('b':9)>),2> <0,('a':<('b':9)>),2> <0,(),2>
7 {[0,1,2]} {$[3][0].b} <0,1,2,<('b':9)>> <0,1,2,<('b':9)>> <0,1,2>
8 {[0,1,2]} {$[1][0].b} <0,1,2> <0,1,2> <0,1,2>
} {
do_execsql_test json101-24.$id.insert {
SELECT json_insert($start,$path,9);
} [list [tx $ins]]
do_execsql_test json101-24.$id.set {
SELECT json_set($start,$path,9);
} [list [tx $set]]
do_execsql_test json101-24.$id.replace {
SELECT json_replace($start,$path,9);
} [list [tx $repl]]
}
finish_test
|