Appending JSON to same file

Appending to a file means seeking to the end of the file and writing
more data to it from that location.

You can just open(filename, "a") to do this (append mode).

This is not possible with JSON files since the start and end of data is
marked by {}.

Technically, a JSON representation of a dict is like that. This is
also legal JSON [1,2,3] and so is this "foo".

Appending additional data after the final } will result in a
malformed JSON file.

A popular format for the OP’s problem is newline delimited JSON, aka
NDJSON. This is a text file with each line containing a single-line JSON
record. Eg:

 { "a": 1, "b": 2 }
 { "a": 3, "b": 4 }
 { "a": 6, "b": 7 }

which would suit the OP well, from the sound of it. In which case a
simple:

 with open("foo.ndjson", "a") as jf:
     print(json.dumps(object_goes_here, separators=(',',':')), file=jf)

will do exactly what they want.

Cheers,
Cameron Simpson cs@cskk.id.au