It's Autumn! Prices fall like leaves! - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Reading JSON files
2. Reading a JSON file as a text file
Summary and Review

Instruction

Now, let's begin by reading a JSON file as a text file. We'll try this on a file containing users' names:

with open('user_name.json', 'r') as file:
  print(file.read())

Let's briefly review this code. The with statement automatically manages closing the files after we are done working with them. The open() function "opens" a file, and the mode ('r', 'w', etc.) is added as an argument to specify what we intend to do with the file (reading, writing, or something else). Then we extract a string containing all characters in the file using read(). If any of this is new to you, we recommend that you check out our course Python Basics Part 2.

Exercise

We have some customer data from our firm's online shops. Let's look at it.

Read the file named customer1.json as a text file into memory and print the file contents.