Skip to content

Instantly share code, notes, and snippets.

@bjourne
Created August 13, 2013 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjourne/6219037 to your computer and use it in GitHub Desktop.
Save bjourne/6219037 to your computer and use it in GitHub Desktop.
Update version numbers
import json
txt = """{
"version" : [0, 1, 0],
"list" : [
{"hi" : 33, "l" : [2, 3, 0], "mo" : { "version" : 3 }},
{"version": [3, 4, 5]}
],
"ad" : { "foo" : 123 }
}"""
def each_obj(o):
yield o
subs = []
if type(o) == dict:
subs = o.values()
elif type(o) == list:
subs = o
for v in subs:
for x in each_obj(v):
yield x
s = json.loads(txt)
for o in each_obj(s):
try:
o['version'][2] += 1
except (KeyError, TypeError):
pass
print s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment