rastackr
rastackr

Reputation: 1

Filter with jsonpath-ng

Working with the following json data:

{
    "data": 
        {
            "level1":
                [
                    {
                        "levelName": "level11",
                        "cost": 1,
                        "child":
                            {
                                "childName": "first",
                                "status": "running"
                            }
                    },
                    {
                        "levelName": "level12",
                        "cost": 2,
                        "child":
                            {
                                "childName": "second",
                                "status": "asleep"
                            }
                    }
                ]
        }
}

A jsonpath search/filter using the expression "$.data.level1[*][?(childName=='first')]" correctly locates the data. However, using the expression "$.data.level1[*][?(levelName=='level11')]" returns blank

How do I search at the "levelName": "level11" level?

In the latter case, if I have the "levelName": "level11" in the json data at the same level as "childName": "first", the search works successfully.

Upvotes: 0

Views: 679

Answers (1)

wp78de
wp78de

Reputation: 18950

If I understand correctly you only need to slightly change your syntax to select the node in question:

$.data.level1[?(@.levelName="level11")]

We are already in the level1 array and can directly filter.

Upvotes: 0

Related Questions