Python (Scripting) - Write a dictionary to a JSON file

by
Jeremy Canfield |
Updated: November 06 2023
| Python (Scripting) articles
Here is an example of how you can write a dictionary to a JSON file using file open and jump_dumps.
#!/usr/bin/python3
import json
dictionary = {
"foo": "Hello",
"bar": "World"
}
try:
json_object = json.dumps(dictionary, indent=4)
except Exception as exception:
print(f"Got the following exception: {exception}")
with open("/path/to/example.json", "w") as file:
file.write(json_object)
file.write("\n")
Did you find this article helpful?
If so, consider buying me a coffee over at