Home > Enterprise >  How to convert csv file into json in python so that the header of csv are keys of every json value
How to convert csv file into json in python so that the header of csv are keys of every json value

Time:07-06

I have this use case

please create a function called “myfunccsvtojson” that takes in a filename path to a csv file (please refer to attached csv file) and generates a file that contains streamable line delimited JSON.

• Expected filename will be based on the csv filename, i.e. Myfilename.csv will produce Myfilename.json or File2.csv will produce File2.json. Please show this in your code and should not be hardcoded.

• csv file has 10000 lines including the header

• output JSON file should contain 9999 lines

• Sample JSON lines from the csv file below:

CSV:

nconst,primaryName,birthYear,deathYear,primaryProfession,knownForTitles
nm0000001,Fred Astaire,1899,1987,"soundtrack,actor,miscellaneous","tt0072308,tt0043044,tt0050419,tt0053137" nm0000002,Lauren Bacall,1924,2014,"actress,soundtrack","tt0071877,tt0038355,tt0117057,tt0037382" nm0000003,Brigitte Bardot,1934,\N,"actress,soundtrack,producer","tt0057345,tt0059956,tt0049189,tt0054452"

JSON lines:

{"nconst":"nm0000001","primaryName":"Fred Astaire","birthYear":1899,"deathYear":1987,"primaryProfession":"soundtrack,actor,miscellaneous","knownForTitles":"tt0072308,tt0043044,tt0050419,tt0053137"}
{"nconst":"nm0000002","primaryName":"Lauren Bacall","birthYear":1924,"deathYear":2014,"primaryProfession":"actress,soundtrack","knownForTitles":"tt0071877,tt0038355,tt0117057,tt0037382"}
{"nconst":"nm0000003","primaryName":"Brigitte Bardot","birthYear":1934,"deathYear":null,"primaryProfession":"actress,soundtrack,producer","knownForTitles":"tt0057345,tt0059956,tt0049189,tt0054452"}

I am not able to understand is how the header can be inputted as a key to every value of jason.

Has anyone come access this scenario and help me out of it?

What i was trying i know loop is not correct but figuring it out

with open(file_name, encoding = 'utf-8') as file:
    csv_data = csv.DictReader(file)
    csvreader = csv.reader(file)
    # print(csv_data)
    keys = next(csvreader)
    print (keys)
    for i,Value in range(len(keys)), csv_data:
        data[keys[i]] = Value
        print (data)

CodePudding user response:

You can convert your csv to pandas data frame and output as json:

df = pd.read_csv('data.csv')
df.to_json(orient='records')

CodePudding user response:

import csv
import json
 
def csv_to_json(csv_file_path, json_file_path):
    data_dict = []
    with open(csv_file_path, encoding = 'utf-8') as csv_file_handler:
        csv_reader = csv.DictReader(csv_file_handler)
        for rows in csv_reader:
            data_dict.append(rows)
    with open(json_file_path, 'w', encoding = 'utf-8') as json_file_handler:
        json_file_handler.write(json.dumps(data_dict, indent = 4))
csv_to_json("/home/devendra/Videos/stackoverflow/Names.csv", "/home/devendra/Videos/stackoverflow/Names.json")
  • Related