Working with JSON
Learn how to store and exchange data using JSON, the language of the web.
Learning Objectives
- Understand what JSON is and why it's used
- Parse JSON strings into Python dictionaries
- Convert Python data into JSON format
- Save and load JSON data from files
The Language of Data
In Day 13, we learned to save text files. But text files can be messy. How do we save complex data like a user profile? We use JSON (JavaScript Object Notation).
JSON is the standard format for exchanging data between applications and the web. It looks almost exactly like a Python dictionary!
The json Module
Python has a built-in module called json to handle this format.
|
|
Python to JSON (Dumps)
When you want to send data or save it, you convert your dictionary back to a JSON string.
|
|
Saving to a File
Instead of strings, we usually work with actual files.
|
|
Interactive Practice
Compare the syntax between Python and JSON. Note how Python’s True becomes true in JSON.
| Feature | Python | JSON |
|---|---|---|
| Boolean | True / False |
true / false |
| Empty Value | None |
null |
| Dictionary | {"key": "value"} |
{"key": "value"} |
| List | [1, 2, 3] |
[1, 2, 3] |
Quiz
Complete this quiz with a minimum score of 80% to mark Day 24 as complete.
Discussion
Have questions or want to discuss this lesson? Join the conversation below!