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