Reputation: 53
I'm very new to programming and picking Python as my first language. I am able to do a "POST" via an API but how do I convert the response I get below to a Python dictionary?
If were to print the response directly I only get:
Response [201]
But if I were to do:
for i in response:
print i
I get:
{"id":"9e1ebc5d","side":"buy","item":"dinosaur","type":"limit","amount":"1.0000","displayAmount":"1.0000","price":"100","createdTime":"2014-12-24T16:01:15.3404000Z","status":"submitted","metadata":{}}
However this is still pretty much useless to me unless I can figure out how convert this into a Python dict.
Upvotes: 4
Views: 69
Reputation: 40814
Use the loads
function from the json
module:
import json
# let x be a string contain the JSON encoded data
x = '{"id":"9e1ebc5d", ...}'
# convert to Python dictionary
p = json.loads(x)
# p is now a Python dictionary
print type(p) # prints <type 'dict'>
print p['id'] # prints 9e1ebc5d
Upvotes: 5