1

I have an anonymous array of objects and I'd like to find an object by a specific field.

I tried this:

jsonPath.get("$.[?(@.name == 'David')]")

but I'm getting the following error:

Invalid JSON expression:
Script1.groovy: 1: Unexpected input: '                         $.[' @ line 1, column 29.
                            $.[?(@.name == 'David')]
                               ^

1 error

How do I fix that?

The json is:

[
 {"name": "David"}, {"name": "Ron"}, {"name": "Dana"}
]
IsaacLevon
  • 2,260
  • 4
  • 41
  • 83

3 Answers3

3

The question is a bit ambiguous, But the syntax is incorrect, Json path syntax uses Groovy's GPath notation

js.getString("find {it.name == 'David'}")
Wilfred Clement
  • 2,674
  • 2
  • 14
  • 29
0

You may need

$.[?(@.name == 'David')]

=>  $.data[?(@.name == 'David')]
    $.response[?(@.name == 'David')]
    $.body[?(@.name == 'David')]
    ...

The name depends on when you extract your response

Rony Nguyen
  • 1,067
  • 8
  • 18
0

Because You are using json Array , do use - $[0] , since david is on first index.

javaHolic
  • 45
  • 7