【问题标题】:Retrieve value by checking certain condition using JsonPath_ng in Python通过在 Python 中使用 JsonPath_ng 检查特定条件来检索值
【发布时间】:2020-08-20 08:37:47
【问题描述】:

我正在创建示例 JSON,如下所示。我想通过在特定条件下使用来检索价值。

观察下面的语句:

'$.Attributes..Attribute[?(@.setcode=="x")]'

但我无法使用代码检索该值。

"{
  "Attributes": [
    {
      "Attribute": {
        "setcode": "x",
        "codeType": "movieType",
        "AttributeGroup": [
          {
            "code": "Force",
            "codeValueValue": "'I'"
          },
          {
            "code": "Remain",
            "codeValueValue": "'P'"
          },
          {
            "code": "Died",
            "codeValueValue": "'E'"
          },
          {
            "code": "Renew",
            "codeValueValue": "'R'"
          }
        ]
      }
      
    },
    {
      "Attribute":{
        "setcode": "y",
        "codeType": "movietype",
        "AttributeGroup": [
          {
            "code": "Force",
            "codeValueValue": "'I'"
          },
          {
            "code": "Remain",
            "codeValueValue": "'P'"
          },
          {
            "code": "Died",
            "codeValueValue": "'E'"
          },
          {
            "code": "Renew",
            "codeValueValue": "'R'"
          }
        ]
      }
    }
  ]
}"

【问题讨论】:

  • 欢迎来到 Stack Overflow。为了获得对您的问题有用的答案,发布带有代码的最少可重现示例,并明确说明您的期望以及发生的事情与您的期望有何不同(包括任何错误消息)会很有帮助。此外,如果您遇到涉及 parse error near token ? 的错误,请查看此 Github 问题:github.com/h2non/jsonpath-ng/issues/16

标签: python jsonpath-ng


【解决方案1】:

虽然您的路径使用 Java (Jayway) 中的通用实现返回某些内容,但我可以确认使用 jsonpath-ng 不会返回任何结果。问题是 "filter op can only be used on lists" 并且看起来需要预先在路径中从可迭代开始:

我们必须过滤Attributes-array,然后拉取Attribute

$.Attributes[?(@.Attribute.setcode=="x")].Attribute

完整代码示例:

import json
json_string = """{
  "Attributes": [
    {
      "Attribute": {
        "setcode": "x",
        "codeType": "movieType",
        "AttributeGroup": [
          {
            "code": "Force",
            "codeValueValue": "'I'"
          },
          {
            "code": "Remain",
            "codeValueValue": "'P'"
          },
          {
            "code": "Died",
            "codeValueValue": "'E'"
          },
          {
            "code": "Renew",
            "codeValueValue": "'R'"
          }
        ]
      }          
    },
    {
      "Attribute":{
        "setcode": "y",
        "codeType": "movietype",
        "AttributeGroup": [
          {
            "code": "Force",
            "codeValueValue": "'I'"
          },
          {
            "code": "Remain",
            "codeValueValue": "'P'"
          },
          {
            "code": "Died",
            "codeValueValue": "'E'"
          },
          {
            "code": "Renew",
            "codeValueValue": "'R'"
          }
        ]
      }
    }
  ]
}"""

json_data = json.loads(json_string)

from jsonpath_ng.ext import parser
query = [x.value for x in parser.parse('$.Attributes[?(@.Attribute.setcode=="x")].Attribute').find(json_data)]
print(json.dumps(query))

【讨论】:

    猜你喜欢
    • 2023-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 2017-11-01
    相关资源
    最近更新 更多