I have Java application which gets JSON strings like this one
{
"name": "testName",
"message": [
"TestMessage."
]
}
What I need is to check if there is a "message" array node which has value of "TestMessage." in any of it's elements.
I have access to com.jway.jsonpath.JsonPath in my code. But it looks that it can only read value of particular node. E.g. like this I will be able to fetch first element of "message" array
JsonPath.read(content, "$.message[0]")
But in this way I will have to fetch all elements of the array into Java and validate them there. Instead I would like to perform this check by JsonPath engine itself.
How can I achive this goal with JsonPath? Is it better to use other library?
You can use the filter operations.
Filters are logical expressions used to filter arrays. A typical filter would be [?(@.age > 18)] where @ represents the current item being processed. More complex filters can be created with logical operators && and ||. String literals must be enclosed by single or double quotes ([?(@.color == 'blue')] or [?(@.color == "blue")]).
like this:
JsonPath.read(json, "$.message[?(@ == "TestMessage.")]");
more can refer the readme.MD of https://github.com/json-path/JsonPath