question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to modify the JSON

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:2
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
kennknowlescommented, Jan 22, 2015

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:

data = ...
pattern = jsonpath_rw.parse('*.just')
new_data = pattern.update(data, lambda x: ...)
more_new_data = pattern.set(data, "some value")
even_more_new_data = pattern.contextual_update(data, lambda context, x: ...)

There are many useful ways to update:

  • A function from old value to new value.
  • A constant value to be set (degenerate form of the above).
  • A function from old value and some context information to new value.

Pull requests welcome 😃

0reactions
ulidtkocommented, Oct 22, 2019

Hi all. It is true that I have no time to maintain this or even really do code review. I am happy to merge a pull request that makes the README point to a newer fork.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found