Skip to content

How to modify the JSON #21

Description

@renard

Hi,

This seem to bee a promising lib for JSON parsing.

How can I modify a JSON structure given a path?

I would like to change:

{'foo': [{'baz': 1}, {'baz': 2}]}

to

{'foo': [{'baz': 3}, {'baz': 2}]}

Given a path: foo[0].baz

I tried:

>>> import jsonpath_rw
>>> json = {'foo': [{'baz': 1}, {'baz': 2}]}
>>> path = jsonpath_rw.parse(r'foo[0].baz')
>>> match = path.find(json)
>>> match
[DatumInContext(value=1, path=Fields('baz'), context=DatumInContext(value={'baz': 1}, path=<jsonpath_rw.jsonpath.Index object at 0x1095a7790>, context=DatumInContext(value=[{'baz': 1}, {'baz': 2}], path=Fields('foo'), context=DatumInContext(value={'foo': [{'baz': 1}, {'baz': 2}]}, path=This(), context=None))))]
>>> match[0].value
1
>>> match[0].value = 5
>>> json
{'foo': [{'baz': 1}, {'baz': 2}]}

I guess I missed something here.

thanks in advance.

Cheers

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions