Search code examples
jsonjsonpath

JsonPath filter query syntax


I'm trying to write a JsonPath query that selects a specific object based on a condition, but either the syntax fails me or I fail the syntax.

Given the below Json object, how would I select the "Data" object containing Dirk Gently's details based on the fact that he uses the "Stumble" method?

{
  "Investigators": [
    {
      "Type": "Legend",
      "Object": {
        "Method": "Investigate",
        "Data": {
          "Name": "Sherlock",
          "Surname": "Holmes"
        }
      }
    },
    {
      "Type": "Visionary",
      "Object": {
        "Method": "Stumble",
        "Data": {
          "Name": "Dirk",
          "Surname": "Gently"
        }
      }
    }
  ],
  "Version": 1
}

I know that I can get to the Method-field like this:

$.Investigators..Object.Method 

I assumed that something like this would work:

  $.Investigators..Object[?(@.Method=="Stumble")].Data 

I'm testing this using: https://jsonpath.com/ to evaluate the query - and I can't seem to get it right.

Am I trying to do something that is not achievable or am I just making a stupid mistake?


Solution

  • Go to this jsonpath online sandbox and try this experssion:

     $.Investigators..Object[?(@.Method=="Stumble")].Data
    

    using either the Jayway or Gatling implementations, the output will be:

    [
       {
          "Name" : "Dirk",
          "Surname" : "Gently"
       }
    ]