0

My json file

{
   "people": []
}

My python code

with open("people.json") as jsonFile:
    load = json.load(jsonFile)
    data = {
    "fname": "Jason",
    "lname": "Scott",
    "age": 32,
    "job": "web developer",
    "spouse": "Jane Scott"
    }

    load["people"].append(data)

jsonFile.close()

I want to add to the list from my python file, with my code above, the json file remains unchanged.

1 Answer 1

5

You haven't written anything back into the file. Open up the file in write mode (open("people.json", r+)), then after you append the data, you'll need to json.dump the new dictionary into the file.

Also, no need to do jsonFile.close() at the end. Your with statement deals with that.

Sign up to request clarification or add additional context in comments.

1 Comment

when dumping the data, it does not add to the list, but rather the end of the file, any idea on a fix?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.