jsoncsvcsvtojson

Creating individual JSON files from a CSV file that is already in JSON format


I have JSON data in a CVS file that I need to break apart into seperate JSON files. The data looks like this: {"EventMode":"","CalculateTax":"Y",.... There are multiple rows of this and I want each row to be a separate JSON file. I have used code provided by Jatin Grover that parses the CVS into JSON:

lcount = 0
  out = json.dumps(row)
  jsonoutput = open( 'json_file_path/parsedJSONfile'+str(lcount)+'.json', 'w')
  jsonoutput.write(out)
  lcount+=1

This does an excellent job the problem is it adds "R": " before the {"EventMode... and adds extra \ between each element as well as item at the end.

Each row of the CVS file is already valid JSON objects. I just need to break each row into a separate file with the .json extension.

I hope that makes sense. I am very new to this all.


Solution

  • It's not clear from your picture what your CSV actually looks like.

    I mocked up a really small CSV with JSON lines that looks like this:

    Request
    "{""id"":""1"", ""name"":""alice""}"
    "{""id"":""2"", ""name"":""bob""}"
    

    (all the double-quotes are for escaping the quotes that are part of the JSON)

    When I run this little script:

    import csv
    
    with open('input.csv', newline='') as input_file:
        reader = csv.reader(input_file)
        next(reader)  # discard/skip the fist line ("header")
    
        for i, row in enumerate(reader):
            with open(f'json_file_path/parsedJSONfile{i}.json', 'w') as output_file:
                output_file.write(row[0])
    

    I get two files, json_file_path/parsedJSONfile0.json and json_file_path/parsedJSONfile1.json, that look like this:

    {"id":"1", "name":"Alice"}
    

    and

    {"id":"2", "name":"bob"}
    

    Note that I'm not using json.dumps(...), that only makes sense if you are starting with data inside Python and want to save it as JSON. Your file just has text that is complete JSON, so basically copy-paste each line as-is to a new file.