python - Changing Json Data, getting TypeError -
edit: turns out whole problem json had been converted dictionary previously, without me realising. , using json.dumps() instead of .loads() mistake.
original q:i have piece of json code have turned dictionary. retrieving json server. when try access inner values of new dictionary change value, a:
typeerror: string indices must integers, not str
this code:
corner_data = json.dumps(r.json()) print corner_data corner_data["geometry"]["corners"] = '{"bottom_right": {"y": "575.531616", "x": "690.547363"}, "top_left": {"y": "-146.739075", "x": "-109.105957"}}'
turn json string python dict using json.loads
(not dumps
!):*
corner_data = json.loads(r.json())
modify data, assigning python dict (not string!):
corner_data['geometry']['corners'] = {'bottom_right': {'y': '575.531616', 'x': '690.547363'}, 'top_left': {'y': '-146.739075', 'x': '-109.105957'}}
encode json:
print json.dumps(corner_data)
* i'm assuming here r.json()
returns string. it's entirely possible r.json()
decodes json string , returns dict, depends on r
is. if that's case, don't need json.loads
here, can omit it.
Comments
Post a Comment