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

3 Answers 3

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'}")
Sign up to request clarification or add additional context in comments.

2 Comments

Not sure I get the syntax. What is it?
it operator in Groovy is similar to @ operator in Jayway
1

You may need

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

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

The name depends on when you extract your response

2 Comments

The array is anonymous (look at the JSON example please)
Only OP know that, but It's should not be anonymous lol
0

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

2 Comments

This is just an example. The placement is arbitrary
Agreed, que was ambiguous.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.