【问题标题】:Update json nodes in Python using jsonpath使用 jsonpath 在 Python 中更新 json 节点
【发布时间】:2020-03-22 06:28:55
【问题描述】:

我正在尝试根据 jsonpath 表达式修改 json 数据:

{
    "SchemeId": 10,
    "nominations": [
        {
            "nominationId": 1
        }
    ]
}

使用类似的东西

from jsonpath_ng import jsonpath, parse
jsonpath_expr = parse('$.SchemeId')
jsonpath_expr.find(data)
updated_json = jsonpath_expr.update(data, 'schemeId': 11)

我想更新SchemeId 值,这应该可以使用https://github.com/h2non/jsonpath-ng,但是没有示例。有没有办法做到这一点?

【问题讨论】:

    标签: python json jsonpath


    【解决方案1】:

    我想出来了,所以我可以在这里分享。 update() 方法更改值。

    from jsonpath_ng import jsonpath, parse
    import json
    data = json.loads('''{"SchemeId": 10, "nominations": [ { "nominationId": 1 } ] }''')
    jsonpath_expr = parse('$.SchemeId')
    jsonpath_expr.find(data)
    jsonpath_expr.update(data, 11)
    print(json.dumps(data, indent=2))
    

    【讨论】:

      猜你喜欢
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 1970-01-01
      • 1970-01-01
      • 2019-02-18
      相关资源
      最近更新 更多