4

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

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

Using something like

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

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?

1 Answer 1

16

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.