简体   繁体   中英

JSONPath: Get root array object using filter of child value

Im trying to get JSONPath expression to filter my JSON and get whole sport object using value of child array.

I have following JSON:

[{

        "name": "Soccer",
        "regions": [{
                "name": "Australia",
                "leagues": [{
                        "name": "Australia league",
                        "inplay": 5,
                    }
                ]
            }
        ]
    }, {

        "name": "Tennis",
        "regions": [{
                "name": "Germany",
                "leagues": [{
                        "name": "Germany league",
                        "inplay": 0,
                    }
                ]
            }
        ]
    }
]

I need to get whole sport object where "inplay == 0" using JsonPath expression.

Result should look like that:

 {

    "name": "Tennis",
    "regions": [{
            "name": "Germany",
            "leagues": [{
                    "name": "Germany league",
                    "inplay": 0,
                }
            ]
        }
    ]
}

Regions and Leagues count can be > 1 Therefore $[?(@.regions[0].leagues[0].inplay == 0)] is not suitable

Tried $[?(@.regions[*].leagues[*].inplay == 0)] but it doesnt work

This works for me

$[?(@.regions[0].leagues[0].inplay == 0)]

Since this is not directly supported (as of now) in JayWay JSONPath we leverage contains as a workaround :

$[?(@.regions..inplay contains '0')]

Note: It may look like contains would work similar to a 'like' operator or instr function but this is not the case here. If the inplay value contains a 0, eg 10 it would not pull the record (according to my tests;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM