1

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

1 Answer 1

1

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']
Sign up to request clarification or add additional context in comments.

8 Comments

In short, you are not parsing the list but directly trying to jump to the dict
ok one more thing, if I use latest version of python (python3) then I am not getting the unicode character in Json. But still the issue of Key Error. Let me add this to question.
responseDetails = json.loads(response.content) print(responseDetails) errorCode = responseDetails['ErrorCode'] @irfan. I am parsing it
yes now I understood the issue, marking your response as answer. Thank you
Good to see that you got the answer. BTW unicode in json got removed in latest versions of python (python 3).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.