Given the below Json input:
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}
I need to select the author field if the author matches a given name for eg. Evelyn Waugh
. I am struggling to write the JsonPath Expression for this. I tried the following with no success. Can anyone suggest the correct expression?
$.author?(@ == 'Evelyn Waugh')
$.?(@.author == 'Evelyn Waugh')
$..?(@.author == 'Evelyn Waugh')
JsonPath expressions always refer to a JSON structure in the same way as XPath expression are used in combination with an XML document. The "root member object" in JsonPath is always referred to as $ regardless if it is an object or array. JsonPath expressions can use the dot–notation.
: operator is the array slice operator, so you can slice collections using the syntax [start:end:step] to return a subcollection of a collection. ( ) operator lets you pass a script expression in the underlying implementation's script language. It's not supported by every implementation of JSONPath, however.
For me @Bernie's answer seems to work:
$[?($.author == 'Evelyn Waugh')]
It works on 3/4 json path executors (unfortunately the one not working is jsonpath.com).
The ones working are:
For those who are wondering as to why is this used and why not a plain comparison is being done here, please continue reading.
In my case I am using wiremock to simulate different server responses based on different input values of json, so for example when "author" is "Evelyn Waugh" then reply with {"books":"10"} and for all other values of "author" reply with, maybe {"books":"20"}
So the option here is only to match with a json path and this is what did the trick.
For those still wondering note that somehow wiremock doesn't work with if and else conditions, if you rely on the else condition (wildcard match) it precedes all the conditions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With