Skip to content

Instantly share code, notes, and snippets.

@xcombelle
Created April 27, 2015 14:59
Show Gist options
  • Save xcombelle/1dcfbb50a62aa4d94194 to your computer and use it in GitHub Desktop.
Save xcombelle/1dcfbb50a62aa4d94194 to your computer and use it in GitHub Desktop.
fixup for chat.python.
import pprint
d=[
{
"exchange": "B",
"price": {
"id": 1,
"value": "false"
}
},
{
"exchange": "C",
"price": {
"id": 2,
"value": "false"
}
}]
def fixup(adict, k, v):
if isinstance(adict,list):
for item in adict:
fixup(item,k,v)
elif isinstance(adict,dict):
for key in adict.keys():
if k == key and adict["id"] == 2:
adict[key] = v
if isinstance(adict[key],dict):
fixup(adict[key], k, v)
fixup(d,"value","true")
pprint.pprint(d)
# result:
#[{'exchange': 'B', 'price': {'id': 1, 'value': 'false'}},
# {'exchange': 'C', 'price': {'id': 2, 'value': 'true'}}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment