JSON Processing
JSON (JavaScript Object Notation) is a lightweight text format for storing and sharing data, common in web apps, APIs, and configs. It's both human-readable and easy for machines to parse.
In Python, the built-in "json"
module lets you convert between JSON and Python objects.
- Show example code that reads
"data.json"
and prints its content in a nicely formatted way. - Show example code that updates the
"age"
in the JSON data and adds a new hobby. Then writes data back to"data.json"
with indentation. - Write a nested JSON file and show example code that reads a nested JSON object from a
nested.json
file and accesses values at different levels.
What JSON Looks Like
JSON is made of key-value pairs (like Python dictionaries) and lists of values.
- Objects use curly braces
{}
with keys in double quotes; - Arrays use square brackets
[]
.
Example:
{
"name": "Alice",
"age": 30,
"hobbies": ["reading", "cycling"]
}
Working with JSON Data
Suppose we have a prepared file "data.json"
with a user's name, age, and hobbies.
Reading and Printing Nicely
Open the file in read mode and use "json.load"
to convert it into a Python object.
You can print it directly, or use "json.dumps(..., indent=4)"
to display it in a readable format.
Updating Values in JSON Data
After loading JSON into a Python dictionary, you can update it like any other dictionary.
For example, modify the "age"
value or append a new hobby to the "hobbies"
list.
Writing Updated Data Back
After changes, open the file in write mode and use "json.dump"
to save the updated dictionary as JSON.
Include the "indent"
parameter to keep the file formatted and easy to read.
Handling Nested JSON Structures
JSON can include nested objects and arrays β dictionaries inside dictionaries or lists with multiple levels.
To access values, combine dictionary keys and list indexes.
For example: "user['address']['city']"
retrieves the city inside the address object.
Summary
- JSON is a lightweight and universal format for storing and sharing data;
- Python's
"json"
module handles reading, writing, and converting JSON; - You can update JSON, format it for readability, and work with nested structures;
- Understanding nested data access is key when working with real-world JSON files.
Try It Yourself
- Create a JSON file named
"profile.json"
with your name, age, and a nested object containing your city and country. - Read it in Python, print the city, update it to a different city, and write the file back with indentation.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you show me an example of how to read and update a JSON file in Python?
How do I access nested values in a JSON object using Python?
What happens if the JSON file is not formatted correctly?
Awesome!
Completion rate improved to 5
JSON Processing
Swipe to show menu
JSON (JavaScript Object Notation) is a lightweight text format for storing and sharing data, common in web apps, APIs, and configs. It's both human-readable and easy for machines to parse.
In Python, the built-in "json"
module lets you convert between JSON and Python objects.
- Show example code that reads
"data.json"
and prints its content in a nicely formatted way. - Show example code that updates the
"age"
in the JSON data and adds a new hobby. Then writes data back to"data.json"
with indentation. - Write a nested JSON file and show example code that reads a nested JSON object from a
nested.json
file and accesses values at different levels.
What JSON Looks Like
JSON is made of key-value pairs (like Python dictionaries) and lists of values.
- Objects use curly braces
{}
with keys in double quotes; - Arrays use square brackets
[]
.
Example:
{
"name": "Alice",
"age": 30,
"hobbies": ["reading", "cycling"]
}
Working with JSON Data
Suppose we have a prepared file "data.json"
with a user's name, age, and hobbies.
Reading and Printing Nicely
Open the file in read mode and use "json.load"
to convert it into a Python object.
You can print it directly, or use "json.dumps(..., indent=4)"
to display it in a readable format.
Updating Values in JSON Data
After loading JSON into a Python dictionary, you can update it like any other dictionary.
For example, modify the "age"
value or append a new hobby to the "hobbies"
list.
Writing Updated Data Back
After changes, open the file in write mode and use "json.dump"
to save the updated dictionary as JSON.
Include the "indent"
parameter to keep the file formatted and easy to read.
Handling Nested JSON Structures
JSON can include nested objects and arrays β dictionaries inside dictionaries or lists with multiple levels.
To access values, combine dictionary keys and list indexes.
For example: "user['address']['city']"
retrieves the city inside the address object.
Summary
- JSON is a lightweight and universal format for storing and sharing data;
- Python's
"json"
module handles reading, writing, and converting JSON; - You can update JSON, format it for readability, and work with nested structures;
- Understanding nested data access is key when working with real-world JSON files.
Try It Yourself
- Create a JSON file named
"profile.json"
with your name, age, and a nested object containing your city and country. - Read it in Python, print the city, update it to a different city, and write the file back with indentation.
Thanks for your feedback!