How to do JSONPath query using a value in the JSON doc

53 Views Asked by At

I have following JSON:

{
    "person": {
        "id": "5678"
    },
    "result": {
        "accounts": [
            {
                "id": "1234",
                "details": [
                    {
                        "name": "abcd"
                    }
                ]
            },
            {
                "id": "5678",
                "details": [
                    {
                        "name": "efgh"
                    }
                ]
            }
        ]
    }
}

I need to find account based on the "id" in the "person" attribute. I tried following: $..accounts[[email protected] == $.person.id]] and it returns nothing. Any help will be greatly appreciated

I tried: $..accounts[[email protected] == $.person.id]]

I was expecting following result:

{
    "id": "5678",
    "details": [
        {
            "id": "5678",
            "name": "efgh"
        }
    ]
}
1

There are 1 best solutions below

0
On

You have an extra ] on the end of your path. When I run

$..accounts[[email protected] == $.person.id]

on my playground, https://json-everything.net/json-path, it works as expected and returns:

[
  {
    "Value": {
      "id": "5678",
      "details": [
        {
          "name": "efgh"
        }
      ]
    },
    "Location": "$['result']['accounts'][1]"
  }
]