JSON Files

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Instruction

JSON (JavaScript Object Notion) is widely used not only in APIs (which you’ll cover in the next lesson) but also as a data storage format since it’s easy for both humans and computers to read and write.

Reading JSON Files

Just as there’s a csv module in the Python Standard Library for working with CSV data, there’s also a json module for doing the same with JSON data.

Handling Decoding Errors

There’s always a chance that the JSON file might not be formatted appropriately. In practice, you’ll want to wrap your file-reading code inside a try block to catch a JSON decoding error, along with other errors that are likely to happen while reading files, as shown below:

Writing JSON Files

To write data to a JSON file, you need to:

Working With JSON Strings

In addition to providing ways to read and write to JSON files, the json module also provides methods for reading and writing data from and to JSON strings.

See forum comments
Download course materials from Github
Previous: CSV Files Demo Next: JSON Files Demo