I'm struggling to append new data to a JSON. I simply want to call JSON data from an API, and put it into a JSON file under "matches". I am creating a blank JSON file containing the following and just going from there.
{
"matches": [
]
}
Here is my code, though I suspect only the last line is important:
print ("No records found...\n Creating new match history bank.")
file_handle = open(all_matches_index, "w+")
file_handle.write('{\n "matches": [\n ]\n}')
for game_id in game_ids:
full_match_data = watcher.match.by_id(my_region, game_id)
#this is the problem line:
file_handle.write(json.dumps({"matches" : full_match_data }, sort_keys=True, indent = 4, separators=(',', ': ')))
I have tried a litany of different solutions and nowhere online seems to address this, or at least I am not understanding what I am reading. I know its a simple problem but I can't solve it.
Some examples of what I have tried:
file_handle.write(json.dumps(full_match_data["matches"], sort_keys=True, indent = 4, separators=(',', ': ')))
file_handle["matches"].write(json.dumps(full_match_data, sort_keys=True, indent = 4, separators=(',', ': ')))
file_handle["matches"] = {**full_match_data, file_handle["matches"] sort_keys=True, indent = 4, separators=(',', ': ')))}
file_handle.write(json.dumps({"matches" : [full_match_data]}, sort_keys=True, indent = 4, separators=(',', ': ')))
file_handle.write(json.dumps(["matches" {full_match_data}], sort_keys=True, indent = 4, separators=(',', ': ')))
Edit 1: Changes based on the response from Pranav Hosangadi,
First of all, thank you for your response, it contains far more information than a simple fix and this really helps me as I learn.
I changed my code so that it now looks as follows:
file_handle = open(all_matches_index, "w+")
file_handle.write('{\n "matches": [\n ]\n}')
file_handle.close()
matches = []
for game_id in game_ids:
full_match_data = watcher.match.by_id(my_region, game_id)
matches.append(full_match_data)
with open(all_matches_index, "w") as file_handle:
json.dump(file_handle, {"matches": matches})
file_handle.close
Unfortunately, it doesn' work. It seems to think for quite a while (being an intel pentium) and then returns an empty file?
Running it a second time fills the file with:
{
"matches": [
]
}
Would you be able to tell me where I am going wrong?
Once I get it to work I will switch to the pythonic way of doing it. Many thanks.