Use the json.dump
function, like this:
import json
data = {"example": "123"}
with open('output.json', 'w') as f:
json.dump(data, f)
The dump
function takes a JSON-serializable object as its first argument,
and a file to write the serialized data into as its second argument.
You can also use json.dumps
to create a string and then manually write it to
the file:
with open('output.json', 'w') as f:
f.write(json.dumps(data))