Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Reading JSON files
10. JSON parsed from a string
Summary and Review

Instruction

We can get data in the form of strings, such as when we download it from the Internet or import it from a database. How can we work with such data in JSON? With the json.loads() function. It looks like this:

import json
json_string = '[{"first_name": "Guido", "last_name":"Rossum"}, {"first_name": "Alfred", "last_name":"Hitchcock"}]'
parsed_json = json.loads(json_string)
print(parsed_json[0]["first_name"])

Here, we loaded the json_string string into a Python object named parsed_json. The json.loads() function creates a dictionary from a JSON string. Note that the extra "s" in "loads" means load from string.

Exercise

Load the order information in json_string with the help of the json.loads() function. Calculate and print the total price of all orders.