1

So I have a function in python which generates a dict like so:

player_data = {
   "player": "death-eater-01",
   "guild": "monster",
   "points": 50
}

I get this data by calling a function. Once I get this data I want to write this into a file, so I call:

g = open('team.json', 'a')
with g as outfile:
  json.dump(player_data, outfile)

This works fine. However my problem is that since a team consists of multiple players I call the function again to get a new player data:

player_data = {
       "player": "moon-master",
       "guild": "mage",
       "points": 250
    }

Now when I write this data into the same file, the JSON breaks... as in, it show up like so (missing comma between two nodes):

{
       "player": "death-eater-01",
       "guild": "monster",
       "points": 50
    }
{
       "player": "moon-master",
       "guild": "mage",
       "points": 250
    }

What I want is to store both this data as a proper JSON into the file. For various reasons I cannot prepare the full JSON object upfront and then save in a single shot. I have to do it incrementally due to network breakage, performance and other issues.

Can anyone guide me on how to do this? I am using Python.

Undefined Variable
  • 4,196
  • 10
  • 40
  • 69

2 Answers2

2

You shouldn't append data to an existing file. Rather, you should build up a list in Python first which contains all the dicts you want to write, and only then dump it to JSON and write it to the file.

If you really can't do that, one option would be to load the existing file, convert it back to Python, then append your new dict, dump to JSON and write it back replacing the whole file.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • One way to make it a valid JSON file is to write a list. On the first line write `"["`. Then alternate your dicta with commas. If you can detect end of data in time, replace the last comma by `"]"` to close the list. If you have already written a comma then add another valid element to the list before closing it. – nigel222 Feb 06 '17 at 11:51
0

To produce valid JSON you will need to load the previous contents of the file, append the new data to that and then write it back to the file.

Like so:

def append_player_data(player_data, file_name="team.json"):
    if os.path.exists(file_name):
        with open(file_name, 'r') as f:
            all_data = json.load(f)
    else:
        all_data = []
    all_data.append(player_data)
    with open(file_name, 'w') as f:
        json.dump(all_data, f)
Raniz
  • 10,882
  • 1
  • 32
  • 64