Parse JSON
Parsing is breaking a block of data into smaller pieces (tokens) by following a set of rules (using delimiters for example), so that this data could be processes piece by piece (managed, analysed, interpreted, transmitted, ets).
How do we parse JSON in Python. First we load a JSON file using json.load() method. The result is a Python dictionary. We can then access the fields using dictionary methods.
JSON is a lightweight data-interchange format.
To extract information from a JSON file or a JSON response, we have to parse the data.
It’s a good idea to convert JSON to a Python dictionary when you want to use the data in your code because this lets you access all of the supporting functions dictionary objects offer in Python. The next example uses json.loads() to convert the JSON into a Python dictionary. It then uses a few of the built in functions of the dictionary object and prints the results to the console:
We will use the following JSON in our example:
{
"orders": [
{
"size": "medium",
"price": 15.67,
"toppings": ["mushrooms", "pepperoni", "basil"],
"extra_cheese": false,
"delivery": true,
"client": {
"name": "Jane Doe",
"phone": null,
"email": "janedoe@email.com"
}
},
{
"size": "small",
"price": 6.54,
"toppings": null,
"extra_cheese": true,
"delivery": false,
"client": {
"name": "Foo Jones",
"phone": "556-342-452",
"email": null
}
}
]
}
Please take a moment to analyze the structure of this JSON file.
Here are some quick tips:
- Notice the data types of the values, the indentation, and the overall structure of the file.
- The value of the main key “orders” is an array of JSON objects (this array will be represented as list in Python). Each JSON object holds the data of a pizza order. If we want to read this file in Python, we just need to read and create a dictionary with the statement:
|
|
json.loads(file) creates and returns a new Python dictionary with the key-value pairs in the JSON file.
Once we have the content of the JSON file stored in the data variable as a dictionary, we can use it to do basically anything we want.
For example, if we write:
print(len(data["orders"]))
We can also use the keys to access their corresponding values. This is what we typically do when we work with JSON files.
For example, to access the toppings of the first order, we would write:
data["orders"][0]["toppings"]
- First, we select the main key “orders”
- Then, we select the first element in the list (index 0).
- Finally, we select the value that corresponds to the key “toppings”
You can see this “path” graphically in the diagram:
If we print this value, the output is:
['mushrooms', 'pepperoni', 'basil']
There are 1000s of APIs for use in software and web development on a variety of topics including sport, jokes, bitcoin etc. Before you can start using an API you need to first need to read the API Documentation to understand what parameters need to be used.
In this example we are using a free weather API that doesn’t require an authentication key and just needs {location} to be replaced.
https://weatherdbi.herokuapp.com/data/weather/{location}
|
|
- What other data can you display from the API?
- Now show it on your APP!