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

Updated:   |  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 Buy Me A Coffee



Comments


Add a Comment


Please enter 1305be in the box below so that we can be sure you are a human.