Search code examples
jsonjsonpath

JSONpath to get a value by filter on another value


I have below JSON and i require to validate the status based on given id in my automation script. For that JSON path require

[
  [
    {
      "id": 9905130204,
      "category": {
        "id": 0,
        "name": "string"
      },
      "name": "doggie",
      "photoUrls": [
        "string"
      ],
      "tags": [
        {
          "id": 0,
          "name": "string"
        }
      ],
      "status": "available"
    },
    {
      "id": 9905130203,
      "name": "Jeffs Doggie11/6/2019 2:44:53 PM",
      "photoUrls": [
        "string"
      ],
      "tags": [],
      "status": "available"
    },
    {
      "id": 9905130217,
      "name": "Goot Doggie",
      "photoUrls": [
        "https://media.karousell.com/media/photos/products/2017/09/14/doggi_door_stopper_1505372529_5cdd1eba0"
      ],
      "tags": [],
      "status": "available"
    }
  ]
]

I want to extract "status": "available" based on "id": 9905130217. No clue how to do that, please help.


Solution

  • You can use the following jsonpath expression to find the status where id = n:

    $.[?(@.id == 'n')].status
    

    So, for your specific case:

    $.[?(@.id == '9905130217')].status
    

    Note, that this assumes id is unique.