Skip to content
This repository has been archived by the owner on Sep 17, 2021. It is now read-only.

Mutating JSON? #6

Closed
kimzander1 opened this issue Jan 9, 2018 · 2 comments
Closed

Mutating JSON? #6

kimzander1 opened this issue Jan 9, 2018 · 2 comments

Comments

@kimzander1
Copy link

Hi this is kinda off topic, but I'm at my wits end and looking for advice.

I'd like to update a JSON object in the vcs representation (replace a page with another page).
My Question is. Is anyone doing this? And how?

Details:
I've been trying to mutate (change) the array of objects in the sections object of Report\Layout
I've been using python jsonpath_rw w/ it's extentions to update the json document. (HERE)

But although I can reference objects with JsonPath successfully - down to the individual ReportPage.
which is a JSON array element of Report\Layout (sections).

I'm not having luck mutating the matched object found with the jsonpath. Seems I can mutate / update the root, with a search of '$' but anything else doesn't change the document and I'm kinda a newbie with all this.

I can get the contents of Report\Layouts sections[] both as a list of ReportPageObjects (my term) and just the sections object that are the parents to the list of ReportPageObjects.
But I'm not able to mutate (basically replace a existing ReportPageObject)

And it's likely bc all this is new to me.... any advice is welcome.

Thank you!

@kodonnell
Copy link
Contributor

Yup off topic = ) Seems like an issue with jsonpath-rw, as per that issue you linked. In normal python you'd just do something like the below (where I've probably got the exact json.load(s)/dump(s) syntax a little off). I.e. load json into an dictionary, mutate the dictionary then write it out.

d = json.load("./Report\\Layout")
d['some']['path']['to']['sections'][0] = {'fruit': 'apples'}
json.dump(d, "./Report\\Layout.new")

Note that doing

x = d['some']['path']['to']['sections'][0]
x = {'fruit': 'apples'}

won't mutate the dictionary.

@kimzander1
Copy link
Author

stilled by this...
Wow...python....
If code can be beautiful, this is.
thanks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants