🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The json.dump function in Python's json module is used to convert a Python object into a JSON string and write it to a file. This function is helpful when you want to save data in a file in JSON format.
Table of Contents
- Introduction
json.dumpFunction Syntax- Examples
- Basic Usage
- Writing to a File
- Pretty-Printing JSON
- Writing JSON with Custom Separators
- Real-World Use Case
- Conclusion
Introduction
The json.dump function converts a Python object (like a dictionary or list) into a JSON string and saves it to a file. This is useful for saving data that you can easily read later or share with other programs.
json.dump Function Syntax
Here's how you use the json.dump function:
import json
json.dump(obj, fp, *, indent=None, separators=None, sort_keys=False)
Parameters:
obj: The Python object you want to convert to JSON.fp: The file where you want to save the JSON string.indent: Optional. Adds spaces to make the JSON string easier to read. Default isNone.separators: Optional. Changes the separators used in the JSON string. Default isNone.sort_keys: Optional. IfTrue, sorts the dictionary keys in the JSON string. Default isFalse.
Returns:
- None. The function writes the JSON string to the file.
Examples
Basic Usage
Here's an example of how to use the json.dump function to save data to a file.
Example
import json
data = {
'name': 'John',
'age': 30,
'city': 'New York'
}
# Writing JSON data to a file
with open('data.json', 'w') as file:
json.dump(data, file)
Output:
A file named data.json is created with the following content:
{"name": "John", "age": 30, "city": "New York"}
Pretty-Printing JSON
This example shows how to make the JSON string easier to read by adding spaces.
Example
import json
data = {
'name': 'John',
'age': 30,
'city': 'New York'
}
# Writing pretty-printed JSON data to a file
with open('data_pretty.json', 'w') as file:
json.dump(data, file, indent=4)
Output:
A file named data_pretty.json is created with the following content:
{
"name": "John",
"age": 30,
"city": "New York"
}
Writing JSON with Custom Separators
This example shows how to change the separators used in the JSON string.
Example
import json
data = {
'name': 'John',
'age': 30,
'city': 'New York'
}
# Writing JSON data with custom separators to a file
with open('data_custom_separators.json', 'w') as file:
json.dump(data, file, separators=(',', ':'))
Output:
A file named data_custom_separators.json is created with the following content:
{"name":"John","age":30,"city":"New York"}
Real-World Use Case
Saving Configuration Data
In real-world applications, the json.dump function can be used to save settings or configuration data to a file. This allows the application to load the settings when it starts.
Example
import json
config = {
'version': 1.0,
'settings': {
'theme': 'dark',
'language': 'en'
},
'user': {
'name': 'Alice',
'email': 'alice@example.com'
}
}
# Saving configuration data to a file
with open('config.json', 'w') as file:
json.dump(config, file, indent=4)
# Loading the configuration data from the file
with open('config.json', 'r') as file:
loaded_config = json.load(file)
print(loaded_config)
Output:
A file named config.json is created with the following content:
{
"version": 1.0,
"settings": {
"theme": "dark",
"language": "en"
},
"user": {
"name": "Alice",
"email": "alice@example.com"
}
}
Conclusion
The json.dump function in Python's json module converts a Python object into a JSON string and writes it to a file. This is useful for saving data in a format that is easy to read and share. Proper use of this function can make your data storage tasks simpler and more efficient.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment