How to Parse JSON Data in Python

In this article, we show how to parse JSON data in Python.
JSON data and Python data look almost identical.
This is a subtle difference but an important one because when you parse JSON data, in order for it to be usable in Python in a form of dictionary, the JSON data must be converted to Python data.
So, to parse JSON data, which is convert JSON data into Python data in the form of a dictionary, you use the json.loads() function. This function takes JSON data (a string) and convert into a dictionary in Python. Now, you can use the data just as if it were created as a dictionary in Python.
So, below, we have JSON data.
So, below, we create a dictionary in Python. We then use the json.dumps() function to convert the Python data into
JSON data. Once we have this JSON-encoded data, we then convert it back to a Python dictionary.
So, in our code, we first import the json module.
We then create a dictionary named person that stores data about a person, whose name is David, who is age 29, and who is black.
If you use the type() function in Python, you will see that this data is a dictionary.
Next, we create a variable called json_string that encodes this Python dictionary data into JSON data using the json.dumps() function. Now if you use the type() function, you will see that this is a string.
Next, we create a variable called pythondata, which converts JSON data into a Python dictionary. Now if you were to use the type() function, you would see that this data is a dictionary.
Again, we can covert any JSON data to Python data using the json.loads() function, specifying the data as the parameter
to this function.
Once the data is converted to a dictionary in Python, you can use it just as if it were originally created as a dictionary in Python.
Related Resources
How to Write JSON Data in Python