Update json nodes in Python using jsonpath

后端 未结 1 487
甜味超标
甜味超标 2021-01-28 01:08

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

{
    \"SchemeId\": 10,
    \"nominations\": [
        {
            \"nominationId\": 1
                 


        
相关标签:
1条回答
  • 2021-01-28 02:03

    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))
    
    0 讨论(0)
提交回复
热议问题