Data not writing correctly to JSON file
Question:
I’m trying to write data to JSON file, but it is not writing it correctly and I can’t find out why
import json
person = {
"Name": "William",
"Age": 30,
"full-time": True
}
personJSON = json.dumps(person, indent=4, sort_keys=True)
with open('person.json', 'w') as file:
json.dump(personJSON, file)
This is what it writes to the file "{n "Age": 30,n "Name": "William",n "full-time": truen}"
I’ve been looking online and the code seems fine to me
Answers:
Do not use .dumps
and .dump
subsequently, just use latter i.e. do
import json
person = {
"Name": "William",
"Age": 30,
"full-time": True
}
with open('person.json', 'w') as file:
json.dump(person, file, indent=4, sort_keys=True)
I’m trying to write data to JSON file, but it is not writing it correctly and I can’t find out why
import json
person = {
"Name": "William",
"Age": 30,
"full-time": True
}
personJSON = json.dumps(person, indent=4, sort_keys=True)
with open('person.json', 'w') as file:
json.dump(personJSON, file)
This is what it writes to the file "{n "Age": 30,n "Name": "William",n "full-time": truen}"
I’ve been looking online and the code seems fine to me
Do not use .dumps
and .dump
subsequently, just use latter i.e. do
import json
person = {
"Name": "William",
"Age": 30,
"full-time": True
}
with open('person.json', 'w') as file:
json.dump(person, file, indent=4, sort_keys=True)