home ~ projects ~ socials

Write a JSON to a File with Python

import json


def write_json_to_file(data, file):
    with open(file, 'w', encoding='utf-8') as _out:
        json.dump(data, _out, sort_keys=True, indent=4)


some_data = {
    "alfa": "bravo",
    "charlie": "delta",
}

a_file = "output/python-json-example.json"

write_json_to_file(some_data, a_file)

print("wrote file")
Output:
wrote file

There is an option to use:

ensure_ascii=False

Need to look into that too.

-- end of line --