Fix Python – How to parse data in JSON format

Question

Asked By – ingh.am

My project is currently receiving a JSON message in python which I need to get bits of information out of. For the purposes of this, let’s set it to some simple JSON in a string:

jsonStr = '{"one" : "1", "two" : "2", "three" : "3"}'

So far I’ve been generating JSON requests using a list and then json.dumps, but to do the opposite of this I think I need to use json.loads. However I haven’t had much luck with it. Could anyone provide me a snippet that would return "2" with the input of "two" in the above example?

Now we will see solution for issue: How to parse data in JSON format


Answer

Very simple:

import json
data = json.loads('{"one" : "1", "two" : "2", "three" : "3"}')
print data['two']  # Or `print(data['two'])` in Python 3

This question is answered By – John Giotta

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0