I would like to create a list of dictionaries in my JSON file. The problem, though, is that JSON does not append new data properly. Here is my code:
import json
json_data = [
{
'name': 'Luke',
'age': 27
},
{
'name': 'Harry',
'age': 32
},
]
with open('data.json', 'a+') as file:
json.dump(json_data, file)
JSON just creates another whole list at each appending attempt:
[
{"name": "Luke", "age": 27},
{"name": "Harry", "age": 32}
][
{"name": "Luke", "age": 27},
{"name": "Harry", "age": 32}
][
{"name": "Luke", "age": 27},
{"name": "Harry", "age": 32}
]
I want to have one single list that holds all these dictionaries. Here's an example of how it SHOULD be like ideally:
[
{"name": "Luke", "age": 27},
{"name": "Harry", "age": 32},
{"name": "Luke", "age": 27},
{"name": "Harry", "age": 32},
{"name": "Luke", "age": 27},
{"name": "Harry", "age": 32},
]
How do I create something like that? Thank you for all your help! :)