# Python program to write JSON
# to a file
import json
# Data to be written
dictionary ={"name":"sathiyajith","rollno":56,"cgpa":8.6,"phonenumber":"9976770500"}withopen("sample.json","w")asoutfile:
json.dump(dictionary, outfile)
On a modern system(i.e.Python3 and UTF-8 support), you can write a nice file with:import json
withopen('data.json','w', encoding='utf-8')asf:
json.dump(data, f, ensure_ascii=False, indent=4)
import json
# python object(dictionary) to be dumped
dict1 ={"emp1":{"name":"Lisa","designation":"programmer","age":"34","salary":"54000"},"emp2":{"name":"Elis","designation":"Trainee","age":"24","salary":"40000"},}
# the json file where the output must be stored
out_file =open("myfile.json","w")
json.dump(dict1, out_file, indent =6)
out_file.close()
import json
# python object(dictionary) to be dumped
dict1 ={"emp1":{"name":"Lisa","designation":"programmer","age":"34","salary":"54000"},"emp2":{"name":"Elis","designation":"Trainee","age":"24","salary":"40000"},}
# the json file where the output must be stored
out_file =open("myfile.json","w")
json.dump(dict1, out_file, indent =6)
out_file.close()