Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Reading JSON files
3. Loading a JSON file into memory
Summary and Review

Instruction

Great! Usually you want to read the JSON file into memory rather than display its contents. The code for loading a JSON file into memory is very similar to the code for reading in a text file:

import json
with open('user_name.json', 'r') as file:
  user = json.load(file)

Once again, we use the with statement and the open() function to open the file. Then we use the json.load() function to load the contents of the JSON file into memory. json.load() returns a Python dictionary:

print(user)
{
  'first_name': 'Sophia',
  'last_name': 'Johnson',
  'user_name': 'sophiaaa'
}

Exercise

Let's look at that customer data again. Load the JSON file named customer1.json into a variable named customer1. Display the keys of the customer1 dictionary (each key on a separate line).