I'm working with a JSON file and I was wondering if there's a way to append a string to a list within the file. Here's an example of the JSON file I'm working with:
{"language": "['English', 'French']", "bank": 50}
I want to add the string "Spanish" to the "language" list. How can I do this?
Here's the code I've written so far, but I'm not sure how to modify it to achieve what I want:
import json
with open("example.json", "r") as jsonFile:
data = json.load(jsonFile)
add list(data["language"]['Spanish'])
with open("example.json", "w") as jsonFile:
json.dump(data, jsonFile)
How can I modify this code to achieve my goal?
data["language"].append('Spanish'), then save back to your file.['English', 'French']in your file is surrounded by double quotes? Because that seems an awkward, probably bad, choice of JSON structure. It also invalidates my suggestion above.{"language": "['English', 'French']", "bank": 50}