Oct-30-2021, 11:23 PM
(Oct-30-2021, 10:18 PM)snippsat Wrote:(Oct-30-2021, 10:12 PM)squad Wrote: If the json file is not empty, the yes code does not give an errorIf start with empty file can not usejson.load(file)
on that file.
Make the the structure you want first with a dictionary,thenjson.dump
to make it json file.
Example.
import json from pprint import pprint def write_json(new_date, filename='d.json'): json_make = {} with open(filename, 'r+') as file: json_make["emp_details"] = new_date file.seek(0) json.dump(json_make, file, indent=4) if __name__ == '__main__': y = { "emp_name": "Nikhil", "email": "[email protected]", "job_profile": "Full Time", } write_json(y)
Output:{ "emp_details": { "emp_name": "Nikhil", "email": "[email protected]", "job_profile": "Full Time" } }
I'm sorry for troubling you. Thank you very much for your patience and help my problem has been solved :) <3