How to Create a Blank JSON File in Python?

Estimated read time 1 min read

In Python, you can create a blank JSON file by using the json module to write an empty dictionary or list to a new file with the “.json” extension. Here’s an example:

import json

# Define an empty dictionary
data = {}

# Define the file name and path
file_name = "blank_json_file.json"

# Write the empty dictionary to the JSON file
with open(file_name, "w") as json_file:
    json.dump(data, json_file)

print(f"Blank JSON file '{file_name}' created successfully!")

In the above example, an empty dictionary data is created. The json.dump() function is then used to write the dictionary to a new JSON file named “blank_json_file.json” in write mode (“w”). The resulting file will contain an empty JSON object {} as the contents.

You can also create a blank JSON file with an empty list [] as the contents by simply initializing the data variable as an empty list instead of an empty dictionary. The process of writing the empty list to a JSON file will remain the same as shown in the example above.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply