Search code examples
pythonjsonpython-3.xjsonlines

Convert JSON LINES file to JSON format?


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?


Solution

  • 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(']')