简体   繁体   English

JSONPath:使用子值过滤器获取根数组 object

[英]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.我试图获取 JSONPath 表达式来过滤我的 JSON 并使用子数组的值获取整个运动 object。

I have following JSON:我有以下 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.我需要使用 JsonPath 表达式获取整个运动 object,其中“inplay == 0”。

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地区和联赛计数可以 > 1 因此$[?(@.regions[0].leagues[0].inplay == 0)]不适合

Tried $[?(@.regions[*].leagues[*].inplay == 0)] but it doesnt work试过$[?(@.regions[*].leagues[*].inplay == 0)]但它不起作用

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 :由于 JayWay JSONPath 不直接支持(截至目前),我们利用contains作为解决方法

$[?(@.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.注意:看起来 contains 的工作方式类似于“like”运算符或instr function,但此处并非如此。 If the inplay value contains a 0, eg 10 it would not pull the record (according to my tests;)如果inplay值包含 0,例如10 ,则不会提取记录(根据我的测试;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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