@@ -425,7 +425,7 @@ public void testNullInput()
425
425
}
426
426
427
427
@ Test
428
- public void testDescendantMemberAccessor ()
428
+ public void testDescendantMemberAccessorWithArrayWrapper ()
429
429
{
430
430
assertThat (assertions .query (
431
431
"""
@@ -434,9 +434,58 @@ SELECT json_query(
434
434
'lax $..c'
435
435
WITH ARRAY WRAPPER)
436
436
""" ))
437
+ .matches ("""
438
+ VALUES cast('[[true,{"c":{"c":null}}],{"c":null},null]'AS varchar)
439
+ """ );
440
+
441
+ // `json_query` must return a single JSON item.
442
+ // `WITH ARRAY WRAPPER` wraps `Diana` and `John` into a single JSON array, which can then be successfully returned.
443
+ assertThat (assertions .query (
444
+ """
445
+ SELECT json_query(
446
+ '{"author":"Diana","id":{"value":1, "author":"John"},"notes":[{"type":1,"comment":"foo"},{"type":2,"comment":null}],"comment":["bar","baz"]}',
447
+ 'lax $..author'
448
+ WITH ARRAY WRAPPER)
449
+ """ ))
437
450
.matches (
438
451
"""
439
- VALUES cast('[[true,{"c":{"c":null}}],{"c":null},null ]'AS varchar)
452
+ VALUES cast('["Diana","John" ]'AS varchar)
440
453
""" );
441
454
}
455
+
456
+ @ Test
457
+ public void testDescendantMemberAccessorWithoutArrayWrapper ()
458
+ {
459
+ // Test `json_query` to find a non-existent member in the JSON.
460
+ assertThat (assertions .query (
461
+ """
462
+ select json_query(
463
+ '{"id":{"value":1},"notes":[{"type":1,"comment":"foo"},{"type":2,"comment":null}],"comment":["bar","baz"]}',
464
+ 'lax $..author'
465
+ omit quotes)
466
+ """ ))
467
+ .matches ("VALUES cast(NULL as varchar)" );
468
+
469
+ // Test `json_query` to find a single descendant member in the JSON.
470
+ assertThat (assertions .query (
471
+ """
472
+ select json_query(
473
+ '{"author":"Diana","id":{"value":1},"notes":[{"type":1,"comment":"foo"},{"type":2,"comment":null}],"comment":["bar","baz"]}',
474
+ 'lax $..author'
475
+ omit quotes)
476
+ """ ))
477
+ .matches ("VALUES cast('Diana' as varchar)" );
478
+
479
+ // Test `json_query` to find multiple descendant members in the JSON.
480
+ // `json_query` must return a single JSON item.
481
+ // `WITHOUT ARRAY WRAPPER` returns `NULL`, which is the default result in case of an error.
482
+ assertThat (assertions .query (
483
+ """
484
+ select json_query(
485
+ '{"author":"Diana","id":{"value":1, "author":"John"},"notes":[{"type":1,"comment":"foo"},{"type":2,"comment":null}],"comment":["bar","baz"]}',
486
+ 'lax $..author'
487
+ omit quotes)
488
+ """ ))
489
+ .matches ("VALUES cast(NULL as varchar)" );
490
+ }
442
491
}
0 commit comments