JsonPath: select a root level field if a condition is met

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 the specified name, for example. Evelyn Waugh. I'm struggling to write a JsonPath expression for this. I tried the following without success. Can anyone suggest the correct expression?

$.author?(@ == 'Evelyn Waugh')
$.?(@.author == 'Evelyn Waugh')
$..?(@.author == 'Evelyn Waugh')
+4
source share
2 answers

try $. [? ($. author == 'Evelyn Waugh')]

+3
source

I suppose you could do this:

$[?($.author === @ && @ == 'Evelyn Waugh')]

But this is a terribly useless request and a mistake if you ask me. Everything goes to hell if there is another property with the name of the author.

, , , . , .

0

All Articles