How to convert CSV file to multiline JSON? How to convert CSV file to multiline JSON? json json

How to convert CSV file to multiline JSON?


The problem with your desired output is that it is not valid json document,; it's a stream of json documents!

That's okay, if its what you need, but that means that for each document you want in your output, you'll have to call json.dumps.

Since the newline you want separating your documents is not contained in those documents, you're on the hook for supplying it yourself. So we just need to pull the loop out of the call to json.dump and interpose newlines for each document written.

import csvimport jsoncsvfile = open('file.csv', 'r')jsonfile = open('file.json', 'w')fieldnames = ("FirstName","LastName","IDNumber","Message")reader = csv.DictReader( csvfile, fieldnames)for row in reader:    json.dump(row, jsonfile)    jsonfile.write('\n')


You can use Pandas DataFrame to achieve this, with the following Example:

import pandas as pdcsv_file = pd.DataFrame(pd.read_csv("path/to/file.csv", sep = ",", header = 0, index_col = False))csv_file.to_json("/path/to/new/file.json", orient = "records", date_format = "epoch", double_precision = 10, force_ascii = True, date_unit = "ms", default_handler = None)


I took @SingleNegationElimination's response and simplified it into a three-liner that can be used in a pipeline:

import csvimport jsonimport sysfor row in csv.DictReader(sys.stdin):    json.dump(row, sys.stdout)    sys.stdout.write('\n')