使用jsonpath更新Python中的json节点 [英] Update json nodes in Python using jsonpath

查看:603
本文介绍了使用jsonpath更新Python中的json节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to modify json data based on a jsonpath expression:

{
    "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 ,但是没有示例.有没有办法做到这一点?

I would like to update the SchemeId value, which should be possible using https://github.com/h2non/jsonpath-ng, however there are no examples. Is there a way to achieve this?

推荐答案

我知道了这一点,因此可以在这里分享. update()方法更改值.

I figured this out so I can share here. The update() method changes the values.

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))

这篇关于使用jsonpath更新Python中的json节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆