How to modify the JSON
See original GitHub issueHi,
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
Issue Analytics
- State:
- Created 9 years ago
- Reactions:2
- Comments:15 (2 by maintainers)
Top Results From Across the Web
how to change json key:value - Stack Overflow
how to change json key:value · 1. Your JSON string is not well formatted. · your JSON string is not correct, cosider using...
Read more >Modifying JSON - Json.NET
This sample loads JSON, modifies T:Newtonsoft.Json.Linq.JObject and T:Newtonsoft.Json.Linq.JArray instances and then writes the JSON back out again.
Read more >JSON | modify an array value of a JSON object - GeeksforGeeks
The array value of a JSON object can be modified. It can be simply done by modifying the value present at a given...
Read more >Modifying JSON - Rows
You'll need to copy and paste the value of the cell into another using Ctrl + Shift + V or Cmd + Shift...
Read more >Modifying JSON data using JSON_MODIFY() in SQL Server
Expression: It is the JSON Data string that we want to update. It might be a variable or a column containing JSON ·...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hello! This is a good point. It is actually where the “rw” in the name of the package comes from: read/write. But I got distracted. The intended design is immutable update:
There are many useful ways to update:
Pull requests welcome 😃
Hey, can you please merge #76 ?
Pointing to a newer fork is not really a good way out… Finding a new maintainer and giving them GitHub + PyPi access is. Still, your being around and responding is appreciated.