I have file (around 6 GB) that each line is JSON.
{"name":"name1", "age":40, "car":null}
{"name":"name2", "age":30, "car":null}
{"name":"name3", "age":30, "car":null}
How can I convert it into a JSON array with Python?
I have file (around 6 GB) that each line is JSON.
{"name":"name1", "age":40, "car":null}
{"name":"name2", "age":30, "car":null}
{"name":"name3", "age":30, "car":null}
How can I convert it into a JSON array with Python?
import fileinput
for line in fileinput.input("test.txt", inplace=True):
if 1 != fileinput.filelineno():
print(',{}'.format(line), end='')
else:
print('[{}'.format(line), end='')
open("test.txt","a").write(']')
Load each line and save it into a list.
with open('file.txt', 'r') as in_file:
lines = [json.loads(line) for line in in_file.readlines()]
Then you can save that to a file like with json.dump()
with open('out.json', 'w') as out_file:
json.dump(lines, out_file)