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]"
    ]
}
Anoop
  • 51
  • 5
  • yourObj['error']['e3'] !== undefined – Plee Oct 18 '21 at 22:11
  • I tried it, Didn't work. I tried it in https://jsonpath.herokuapp.com/ – Anoop Oct 18 '21 at 22:23
  • 1
    Based on this answer https://stackoverflow.com/a/52977857/1065197. I tested using `$[?(@.error[?(@ == '[e3]')] != [])]` and Jayway JsonPath evaluator. – Luiggi Mendoza Oct 18 '21 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. – Anoop Oct 18 '21 at 22:58
  • That's not supported from JsonPath, you will always receive a json. What you could do is to check if the result json is empty or not. – Luiggi Mendoza Oct 18 '21 at 23:37
  • 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 – Akshay G Oct 19 '21 at 07:09

1 Answers1

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

Akshay G
  • 2,070
  • 1
  • 15
  • 33