The json Module
Lesson 3 of 9 in Coddy's Python JSON course.
The json module is a built-in Python library that provides functions for working with JSON data. It allows you to encode Python objects into JSON strings and decode JSON strings into Python objects.
Importing the json Module
To use the JSON module in Python, you need to import it first:
import json
Main Functions
The json module provides four main functions:
json.dumps(): Converts a Python object to a JSON stringjson.loads(): Parses a JSON string and returns a Python objectjson.dump(): Writes a Python object to a JSON filejson.load(): Reads a JSON file and returns a Python object
Basic Usage
Here's a quick example of how to use json.dumps() and json.loads():
import json
# Python dictionary
data = {
"name": "John",
"age": 30,
"city": "New York"
}
# Convert Python object to JSON string
json_string = json.dumps(data)
print(json_string)
# Parse JSON string back to Python object
parsed_data = json.loads(json_string)
print(parsed_data)
This module is essential for handling JSON data in Python, whether you're working with APIs, configuration files, or data exchange between different systems.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyCreate a program that demonstrates the use of all four main functions of the json module: dumps(), loads(), dump(), and load().
- Create a Python dictionary with the following key-value pairs:
- "name": "Python"
- "year": 1991
- "creator": "Guido van Rossum"
- "is_oop": true
- "versions": ["2.7", "3.6", "3.7", "3.8", "3.9"]
- Use
json.dumps()to convert this dictionary to a JSON string. Print the result. - Use
json.loads()to parse the JSON string back into a Python object. Print the type of the object. - Use
json.dump()to write the dictionary to a file named "python_info.json". - Use
json.load()to read the contents of "python_info.json" into a new Python object. - Print the value associated with the "creator" key from this new object.
The following input will be provided:
Python
1991
Guido van Rossum
true
2.7,3.6,3.7,3.8,3.9
Note: The versions input is a comma-separated string that should be converted to a list in the Python dictionary.
Try it yourself
import json
# Read input
name = input()
year = int(input())
creator = input()
is_oop = input().lower() == 'true'
versions = input().split(',')
# Create the dictionary
python_info = {
"name": name,
"year": year,
"creator": creator,
"is_oop": is_oop,
"versions": versions
}
# TODO: Write your code below to complete the challenge
# 1. Use json.dumps() to convert the dictionary to a JSON string
# 2. Use json.loads() to parse the JSON string back into a Python object
# 3. Use json.dump() to write the dictionary to a file named "python_info.json"
# 4. Use json.load() to read the contents of "python_info.json"
# 5. Print the value associated with the "creator" key from the loaded object
# Print your results here
print() # TODO: Replace this with your output