JSONPath 根据子过滤器返回父元素

问题描述 投票:0回答:1

我正在尝试找到一个 JSONPath 表达式来获取集合中每个项目的 Itemid(如果 Customer==123 但 JSONPath 正在逃避我)。这是 JSON:

{

"Item:7823yrhfjbafuuowegfuwvbsvbhjbauagwoeufgaus": {
    "Itemid": "7823yrhfjbafuuowegfuwvbsvbhjbauagwoeufgaus",
    "Customers": [
        {
            "Customer": 123
        },
        {
            "Customer": 456
        }
    ]
},
"Item:1234yrhfjbafuuowegfuwvbsvbhjbauagwoeufgdan": {
    "Itemid": "1234yrhfjbafuuowegfuwvbsvbhjbauagwoeufgdan",
    "Customers": [
        {
            "Customer": 123
        },
        {
            "Customer": 456
        }
    ]
}

}

我尝试过 $..Itemid[?($..Customers[?(@.Customer==123)])] 和大量变体,但没有一个对我有用。有什么想法吗?

json jsonpath
1个回答
0
投票

使用嵌套过滤器,您的方向是正确的,但您需要对每个过滤器使用

@
$
引用数据根。

$[[email protected][[email protected]==123]].Itemid
$                                // root 
 [                               // find values
  ?                              // where
   @.Customers                   // Customers
              [                  // has values
               ?                 // where
                @.Customer==123  // Customer is 123
              ]
 ]
 .Itemid                         // get Itemid

我还没有测试过这个(但稍后会),但你可以在https://json-everything.net/json-path尝试一下。

© www.soinside.com 2019 - 2024. All rights reserved.