Snatch any course you want for just $15! Go to “Courses” and find a course ↓
Introduction
Writing to a JSON file
3. json.dumps() options—the indent
Summary

Instruction

Good job! As you can see, the result is not very readable; it's shown all on one line. Let's parameterize it, which is a way of making it print more legibly.

First of all, we can use the indent option to format the displayed result, like this:

print(json.dumps(data, indent = 4))

The indent parameter allows us to format the JSON array elements and object members in a more organized manner.

A positive integer indent indent represents the number of spaces per level that should be used to indent the content. An indent level of 0 or negative will only insert newlines. None (the default) selects the most compact representation.

Let's have a look at the result.

Exercise

We have saved some information in a Python object named data. Using json.dumps(), convert it to a string. Set the indent option to 4. Print the result.