1

I have a json as below. I want a json path expression that checks if [e3] exists in the below json. Can someone help please?

{
    "status": "ok",
    "error": [
        "[e1]",
        "[e2]",
        "[e3]",
        "[e4]"
    ]
}
6
  • yourObj['error']['e3'] !== undefined Commented Oct 18, 2021 at 22:11
  • I tried it, Didn't work. I tried it in jsonpath.herokuapp.com Commented Oct 18, 2021 at 22:23
  • 1
    Based on this answer stackoverflow.com/a/52977857/1065197. I tested using $[?(@.error[?(@ == '[e3]')] != [])] and Jayway JsonPath evaluator. Commented Oct 18, 2021 at 22:39
  • Yeah Luiggi. That works. It returns the whole json if [e3] is present. Is there a way to return 'yes' or something more specific? Returning [e3] itself is also fine. Thank You. Commented Oct 18, 2021 at 22:58
  • 1
    @LuiggiMendoza Jsonpath doesn’t always return json. If the path is definite you can actually cast it to the expected return type. $.status and $.error[2] returns string Commented Oct 19, 2021 at 7:09

1 Answer 1

1

The below JSONPath will return a list with one element. Indefinite paths always returns a list. A path is indefinite if it contains an expression - ?(<expression>). Refer What is Returned When?

$.error[?(@ == '[e3]')]

Jayway JsonPath

Sign up to request clarification or add additional context in comments.

2 Comments

List<String> errorCodes = JsonPath.read(json, "$.error[?(@ == '[e3]')]");
That is just what I want. Thank You, Akshay.

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.