#1043·JsonPath

Giving incorrect results when the json path is nested

Author: kavyaNamballaCreated Jun 24, 2025Updated Jul 14, 2026

Sample Json:

`[

{
    "id": 1,
    "name": "Store A",
    "departments": [
        {
            "id": 10,
            "name": "Electronics",
            "products": [
                {
                    "id": 101,
                    "name": "TV",
                    "in_stock": true
                },
                {
                    "id": 102,
                    "name": "Laptop",
                    "in_stock": false
                }
            ]
        },
        {
            "id": 11,
            "name": "Clothing",
            "products": [
                {
                    "id": 201,
                    "name": "Shirt",
                    "in_stock": true
                },
                {
                    "id": 202,
                    "name": "Jeans",
                    "in_stock": true
                }
            ]
        }
    ]
},
{
    "id": 2,
    "name": "Store B",
    "departments": [
        {
            "id": 12,
            "name": "Grocery",
            "products": [
                {
                    "id": 301,
                    "name": "Milk",
                    "in_stock": false
                },
                {
                    "id": 302,
                    "name": "Eggs",
                    "in_stock": true
                }
            ]
        }
    ]
}

]`

// This should return only "Store B" but returns both stores

Object result = JsonPath.read(json, "$[?(@..products[?(@.id==302)])].name"); Expected: ["Store B"], Actual: ["Store A", "Store B"]

I've tried with this path also,

Object result = JsonPath.read(json, "$[?(@.departments[?(@.products[?(@.id==302)])])].name"); Expected: ["Store B"], Actual: ["Store A", "Store B"]