javajsonrest-assuredrest-assured-jsonpathjayway

JsonPath: find an element in an array by field


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"}
]

Solution

  • 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'}")