Search code examples
jsonjsonpath

Filtering JSON using JSONPath


I'm using a JSON like this one

[
    [{
        "tool": "one",
        "products": [
            "Prod1",
            "Prod2",
            "Prod3",
            "Prod4",
            "Prod5",
            "Prod6"
        ],
        "count": 6
    }],
    [{
        "tool": "two",
        "products": [
            "Prod1",
            "Prod7",
            "Prod3",
            "Prod8",
            "Prod5",
            "Prod9"
        ],
        "count": 6
    }],
    {
        "tool": "three",
        "products": [
            "Prod10",
            "Prod70",
            "Prod30"
        ],
        "count": 3
    }
]

and I'd like to extract all the Prod* that are about the tool "one" so the list

"Prod1",
"Prod2",
"Prod3",
"Prod4",
"Prod5",
"Prod6"

and then the same about the tools "two" and "three".

I'm in troubles with the right filter to use to obtain the result.

Any suggestion will be appreciated and thank you in advance


Solution

  • $..[?(@.tool=="one")] should filter the node that you are interested. you can then access the products list using

    $..[?(@.tool=="one")].products.*
    

    Use https://jsonpathfinder.com/ to find the exact output path and then use https://jsonpath.com/ to try different jsonpath-expressions