无法使用JSONPath语法获取嵌套数组中的JSON元素

huangapple go评论51阅读模式
英文:

unable to get JSON elements inside nested array using jsonpath syntax

问题

我正在尝试获取位置为USA的部门ID,以下是JSON中的部分内容:

{
    "datapoints": [{
        "id": "default.1",
        "definedBy": "default/0.1",
        "featureValues": {
            "bui.displayname": "Health status",
            "bui.visibility": "normal",
            "default.access": "r",
            "default.basetype": "text",
            "default.description": "Aggregated health status",
            "default.format": "text/plain",
            "default.name": "health_status",
            "default.restriction": "re:(OK|WARN|ERROR|UNKNOWN)",
            "name": [
                {
                    "brand": "apple",
                    "company": [
                        {
                            "location": "UAS"
                        }
                    ],
                    "manager": "stev"
                }
            ]
        }
    }, {
        "id": "kdl.240",
        "definedBy": "kdl/0.9",
        "featureValues": {
            "bui.displayname": "Delta K",
            "bui.visibility": "normal",
            "default.access": "rw",
            "default.basetype": "real",
            "default.description": "Delta K",
            "default.name": "Delta_K",
            "default.privacy": "false",
            "default.restriction": "b32",
            "name": [
                {
                    "brand": "apple",
                    "company": [
                        {
                            "location": "canada"
                        }
                    ],
                    "manager": "abc"
                }
            ]
        }
    }]
}

我尝试过以下JSONPath表达式:

$.datapoints[?(@.featureValues.name[0].company[*].location == 'USA')].id

但是这并没有匹配成功。我在https://jsonpath.com/上执行了这个测试。

英文:

I am trying to get the departments id where location is USA in the following JSON

{
    "datapoints": [{
            "id": "default.1",
            "definedBy": "default/0.1",
            "featureValues": {
                "bui.displayname": "Health status",
                "bui.visibility": "normal",
                "default.access": "r",
                "default.basetype": "text",
                "default.description": "Aggregated health status",
                "default.format": "text/plain",
                "default.name": "health_status",
                "default.restriction": "re:(OK|WARN|ERROR|UNKNOWN)",
              "name" : [ 
                            { 
                                    "brand": "apple" ,
                                      "company": [
                                                  {
                                                 "location": "UAS"
                                                  }
                                            ],
                                     "manager": "stev"
                            } 
                         ]
            }
        }, {
            "id": "kdl.240",
            "definedBy": "kdl/0.9",
            "featureValues": {
                "bui.displayname": "Delta K",
                "bui.visibility": "normal",
                "default.access": "rw",
                "default.basetype": "real",
                "default.description": "Delta K",
                "default.name": "Delta_K",
                "default.privacy": "false",
                "default.restriction": "b32",
              "name" : [ 
                            { 
                                    "brand": "apple" ,
                                    "company": [
                                                {
                                                 "location": "canada"
                                                }
                                             ],
                                     "manager": "abc"
                            } 
                         ]
            }
        }
    ]
}

I tried

> $.datapoints[?(@.featureValues.name[0].company[*].location == 'USA')].id

but this gives me not matched. I perform this test on https://jsonpath.com/

答案1

得分: 1

我不确定是否可以在单个请求中完成这个任务,而且我猜结果会因为你没有在问题中指定具体的实现方式而有所不同。

但是你可以使用以下查询来查找符合你的条件的路径:

$..[$.datapoints[?(@.featureValues)].featureValues.name[?(@.company)].company[?(@.location =='USA')]]

例如,在JavaScript中,你可以使用 jp.nodes(obj, pathExpression[, count]) 函数。

一旦你得到了路径,你可以获取匹配的节点并检索你要查找的 ID。

在JavaScript中,你仍然可以使用 jp.nodes(obj, pathExpression[, count]) 函数。

注:在你的示例数据中,我猜有一个小错误,因为 "USA" 被写成了 "UAS"。

英文:

I don't know if this is doable in a single request and I guess the results will be different between the implementation you are using (as you didn't specified any in your question)

However you can use the following query to look for the path(s) matching your criterias

$..[$.datapoints[?(@.featureValues)].featureValues.name[?(@.company)].company[?(@.location =='USA')]

For example in JS jp.nodes(obj, pathExpression[, count])

Once you got the path(s), you can get the matching node(s) and retrieve the id you are looking for.

Still in JS something you can use jp.nodes(obj, pathExpression[, count])

NB: data in your example, i guess there is a little typo as USA is written UAS

答案2

得分: 0

为了保证完整性,随着即将到来的IETF规范,可以利用表达式中的嵌套路径来查找为您找到的JSON Path。

$.datapoints[?@.featureValues.name[?@.company[?@.location=='USA']]].id

将其分解如下:

$.datapoints                      // 查找datapoints中的值
 [?                               // 其中
   @.featureValues.name           // featureValues.name中的值
     [?                           // 其中
       @.company                  // 公司中的值
         [?                       // 其中
           @.location=='USA'     // 位置为'USA'
         ]
     ]
 ]
.id                               // 并获取它们的id

这可能受到一些其他在线评估器的支持,但它绝对受到我的支持,网址是https://json-everything.net/json-path,该网站已经实现了即将推出的规范。

英文:

For completeness, with the IETF specification upcoming, the JSON Path that finds this for you takes advantage of nested paths inside the expression.

$.datapoints[?@.featureValues.name[?@.company[?@.location=='USA']]].id

Breaking this down

$.datapoints                      // find values in datapoints 
 [?                               // that have
   @.featureValues.name           // values in featureValues.name
     [?                           // that have
       @.company                  // values in company
         [?                       // that have
           @.location=='USA'      // location = 'USA'
         ]
     ]
 ]
.id                               // and get their id

This might be supported by some other online evaluators, but it's definitely supported by mine, https://json-everything.net/json-path, which already implements the pending specification.

huangapple
  • 本文由 发表于 2023年7月11日 08:48:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定