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

Asked By: Pryte

||

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)
Answered By: Daweo
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.