I have a Python function that generates a JSON output. I am trying to see how could I write this to a file.
{'logs': ['3982f208'], 'events': [{'sequence_number': 06972977357, 'labels': [], 'timestamp': 1556539666498, 'message': 'abc [2015-12-19 12:07:38.966] INFO [b5e04d2f5948] [ProcessController] PRMS: {"MessageType"=>"TYPE1", "Content"=>{"Name"=>"name1", "Country"=>"europe", "ID"=>"345", "Key1"=>"634229"}}}
I tried the below:
def output(): <-- This function returns the above JSON data
json_data()
I tried to write it but creating a new function as below:
def write():
f = open('results.txt', 'a'). <<- Creates the text file
f.write(json_data)
f.close()
This only creates an empty file. Could anyone advice as to how could I write the JSON data to the file.