This is essentially alexwlchan's comment spelled out:
Parsing and serializing JSON doesn't come for free, so you may want to avoid it. I think you can just output
"[", the first file,
",", the second file etc.,
"]" and call it a day. If all inputs are valid JSON, unless I'm terribly mistaken, this should also be valid JSON.
In code, version 1:
def cat_json(outfile, infiles):
.write("[%s]" % (",".join([mangle(file(f).read()) for f in infiles])))
def cat_json(output_filename, input_filenames):
with file(output_filename, "w") as outfile:
first = True
for infile_name in input_filenames:
with file(infile_name) as infile:
first = False
The second version has a few advantages: its memory requirements should be something like the size of the longest input file, whereas the first requires twice the sum of all file sizes. The number of simultaneously open file handles is also smaller, so it should work for any number of files.
with, it also does deterministic (and immediate!) deallocation of file handles upon leaving each
with block, even in python implementations with non-immediate garbage collection (such as pypy and jython etc.).