How to Create a JSON File in Python
To create a JSON file in Python, you can use the json module.
The following example shows how to create a JSON file in Python.
Using json Module
We can use the json module to create a JSON file.
Suppose we have the following data:
import json
# Declare data
data = {
"Date": ["01-03-2023", "01-03-2023", "01-03-2023", "01-03-2023", "02-03-2023", "02-03-2023", "02-03-2023", "02-03-2023", "03-03-2023", "03-03-2023"],
"Product_Code": ["A-101", "A-102", "A-103", "B-101", "B-102", "B-104", "B-105", "B-106", "C-101", "C-102"],
"Product_Name": ["Laptop", "Mobile", "Printer", "Keyboard", "Scanner", "Mouse", "Laptop", "Printer", "Keyboard", "Mouse"],
"Price": [4500, 550, 250, 50, 350, 50, 3000, 300, 100, 60],
"Status": [1, 1, 1, 0, 1, 1, 1, 0, 1, 0]
}
# Create a JSON file
with open('data.json', 'w') as file:
json.dump(data, file)
In this example, we use the json module to create a JSON file named data.json
and write the data to it.
The output shows the JSON file created with the specified data.
Conclusion
We can use the json module to create a JSON file in Python.