-
Notifications
You must be signed in to change notification settings - Fork 201
How to modify the JSON #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Finally I end up with something like that: import jsonpath_rw
def get_path(match):
'''return an iterator based upon MATCH.PATH. Each item is a path component,
start from outer most item.'''
if match.context is not None:
for path_element in get_path(match.context):
yield path_element
yield str(match.path)
def update_json(json, path, value):
'''Update JSON dictionnary PATH with VALUE. Return updated JSON'''
try:
first = next(path)
# check if item is an array
if first.startswith('[') and first.endswith(']'):
try:
first = int(first[1:-1])
except ValueError:
pass
json[first] = update_json(json[first], path, value)
return json
except StopIteration:
return value
json = {'foo': [{'baz': 1}, {'baz': 2}], 'bar': { 'just': 'a', 'dict': 'object'}}
path = jsonpath_rw.parse(r'*.just')
matches = path.find(json)
print json
for match in matches:
full_path = '/'.join(get_path(match))
json = update_json(json, get_path(match), full_path)
print json {'foo': [{'baz': 1}, {'baz': 2}], 'bar': {'dict': 'object', 'just': 'a'}}
{'foo': [{'baz': 1}, {'baz': 2}], 'bar': {'dict': 'object', 'just': 'bar/just'}} Maybe this could be a feature for |
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:
Pull requests welcome :-) |
@renard I'm also have to use such method. How can I change values using jsonpath? |
Hi! Is there any progress on it? :) I'm getting Not implemented error, when trying to use jsonpath_expr.update(my_json, "some_value") |
There is a way to update json, but it is not documented and wrapped into a пн, 18 янв. 2016, 15:40, petr0ff [email protected]:
|
Nice, thanks! Actually I've tried @renard 's solution and it worked for me |
@petr0ff well, as I can see from PC now, joshbenner have implemented the normal update method (#28). But just in case if someone could be interested, I post here my solution: def string_to_json (self, source):
try:
load_input_json=json.loads(source)
except ValueError, e:
raise Exception("Could not parse '%s' as JSON: %s"%(source, e))
return load_input_json
def update_json(self, json_string, expr, value, index=0):
load_input_json=self.string_to_json (json_string)
matches = self._json_path_search(load_input_json, expr)
datum_object = matches[int(index)]
if not isinstance(datum_object, DatumInContext):
raise Exception("Nothing found by the given json-path")
path = datum_object.path
if isinstance(path, Index):
datum_object.context.value[datum_object.path.index] = value
elif isinstance(path, Fields):
datum_object.context.value[datum_object.path.fields[0]] = value
return load_input_json |
@PositiveAlex, How are you using this without changing anything? I don't see _json_path_search defined anywhere. Are you inheriting another class from this? |
This allows updates with the |
Hi... I have been looking for a solution to this too |
Hey @kennknowles,
This is indeed very nice, but would you care to release this functionality? It's available in master branch only; the latest release 1.4.0 lacks the code. It's not that |
Ah, I see. They already implemented this, in a fork. Switch to https://github.com/h2non/jsonpath-ng |
Hi, maybe it is better to close all issues here and change readme that this repo is deprecated. |
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. |
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:
to
Given a path:
foo[0].baz
I tried:
I guess I missed something here.
thanks in advance.
Cheers
The text was updated successfully, but these errors were encountered: