使用 jsonpath 更新 Python 中的 json 个节点

Update json nodes in Python using jsonpath

我正在尝试根据 json 路径表达式修改 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,但是没有示例。有办法实现吗?

我想出了这个所以我可以在这里分享。 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))