Reputation: 430
my response content is
{"VoucherQuantityResult":[{"ErrorCode":"E057","ErrorMessage":"IP not registered","Message":"IP not registered","QuantityResponse":[],"ResultType":"FAILED"}]}
responseDetails = response.json() results
{u'VoucherQuantityResult': [{u'ErrorCode': u'E057', u'ResultType': u'FAILED', u'Message': u'IP not registered', u'ErrorMessage': u'IP not registered', u'QuantityResponse': []}]}
then if I call, errorCode = responseDetails['ErrorCode']['ResultType']
there is error KeyError: 'ErrorCode'
I have even used the dumps function, json.dumps(response.json())
then the error is,
TypeError: string indices must be integers, not str
Can some one suggest me how to fetch the keys and values without unicode?
Note: if I use python3 then I dont see any unicode character. still the issue
errorCode = responseDetails['ErrorCode']['value']
KeyError: 'ErrorCode'
Thanks
Upvotes: 1
Views: 384
Reputation: 5075
If you see, your response is a list not a dict which contains dict as its items.
So you can access it like this;
errorCode = responseDetails[0]['ErrorCode']['ResultType']
OR
If this is your whole result;
{u'VoucherQuantityResult': [{u'ErrorCode': u'E057', u'ResultType': u'FAILED', u'Message': u'IP not registered', u'ErrorMessage': u'IP not registered', u'QuantityResponse': []}]}
Then use this;
responseDetails['VoucherQuantityResult'][0]['ErrorCode']['ResultType']
Upvotes: 1