TMC - 11 Digital Tech | Semester 1
Toggle Dark/Light/Auto modeToggle Dark/Light/Auto modeToggle Dark/Light/Auto mode

Parse JSON

What is Parsing

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.

Parse JSON in Python

Convert JSON to a Python Dictionary

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:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    def jsonpractice(self):
        file = """{
	"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
			}
		}
	]
}
"""
        data = json.loads(file)

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.

Examples

For example, if we write:

print(len(data["orders"]))
The output is 2 because the value of the main key “orders” is a list with two elements.

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']

Let’s look at some live weather data

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}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
    def btn(self):
        #Set the location we are looking for
        location = "Adelaide"

        # GET request to web server for data in json format and add the location to the end
        response = requests.get("https://weatherdbi.herokuapp.com/data/weather/" + location)

        # print the data recieved from the server
        print(response.json())

        # create a formatted string of the Python JSON object
        text = json.dumps(response.json(), sort_keys=True, indent=4)
        print(text)
        print("---------------------------------------------------")

        # convert the JSON file into a Python dictionary to extract data
        json_data = json.loads(response.text)
        
        # print the current temperature of the location in degrees celsius
        print(json_data["currentConditions"]["temp"]["c"])
        todays_temp = json_data["currentConditions"]["temp"]["c"]
        print("Current temperature is " + str(todays_temp))
  • What other data can you display from the API?
  • Now show it on your APP!