how to convert json to csv in python

19,550

Solution 1

BankData is a dict you do not need to iterate it. You can directly access the values using the key.

Ex:

import csv
import json

with open('/Users/vrindabv/Documents/PycharmProjects/BankStatementEngine/test.json') as infile:
    BankData = json.loads(infile.read())

with open("/Users/vrindabv/Documents/PycharmProjects/BankStatementEngine/test.csv", "w") as outfile:
    f = csv.writer(outfile)
    f.writerow(["userID", "Is salary credited before 5th", "Avg Salary of last 3 months", "Avg Salary of last 6 months", "Avg Balance before salary of last 3 months", "Avg Balance before salary of last 6 months"])
    f.writerow([BankData["userID"], BankData["Is salary credited before 5th"],
                BankData["Avg Salary of last 3 months"],
                BankData["Avg Salary of last 6 months"],
                BankData["Avg Balance before salary of last 3 months"],
                BankData["Avg Balance before salary of last 6 months"]])

Solution 2

You can also use pandas to handle dataframe,

dct = {"userID": "679d3bad-155e-4b39-9ff7-7d564f408942", "Is salary credited before 5th": "Yes", "Avg Salary of last 3 months": 15453.33,
       "Avg Salary of last 6 months": 15290.5, "Avg Balance before salary of last 3 months": 113.15, "Avg Balance before salary of last 6 months": 105.22}

import pandas as pd

df = pd.DataFrame.from_records(dct, index=[0])

df.to_csv('outputfile.csv')
Share:
19,550
vrinda
Author by

vrinda

ML / Knowledge Engineer who focuses on developing algorithms which can “learn” from or adapt to the data and make predictions. Core focuses of interest currently include: Machine learning, Deep Learning, Computer Vision, Natural language processing (NLP) Ontology modelling, Knowledge-Based engineering, Artificial Neural Network.

Updated on June 08, 2022

Comments

  • vrinda
    vrinda almost 2 years

    Following is my json file input

    {"userID": "679d3bad-155e-4b39-9ff7-7d564f408942", "Is salary credited before 5th": "Yes", "Avg Salary of last 3 months": 15453.33, "Avg Salary of last 6 months": 15290.5, "Avg Balance before salary of last 3 months": 113.15, "Avg Balance before salary of last 6 months": 105.22}
    

    Code

        with open('/Users/vrindabv/Documents/PycharmProjects/BankStatementEngine/test.json', "r") as f:
            BankData = json.loads(f.read())
        x = json.loads(json.dumps(BankData))
        f = csv.writer(open("/Users/vrindabv/Documents/PycharmProjects/BankStatementEngine/test.csv", "w"))
        f.writerow(["userID", "Is salary credited before 5th", "Avg Salary of last 3 months", "Avg Salary of last 6 months", "Avg Balance before salary of last 3 months", "Avg Balance before salary of last 6 months"])
    
        for y in x:
            f.writerow([x["userID"], x["Is salary credited before 5th"],
                        x["Avg Salary of last 3 months"],
                        x["Avg Salary of last 6 months"],
                        x["Avg Balance before salary of last 3 months"],
                        x["Avg Balance before salary of last 6 months"]])
    

    Output

    userID,Is salary credited before 5th,Avg Salary of last 3 months,Avg Salary of last 6 months,Avg Balance before salary of last 3 months,Avg Balance before salary of last 6 months
    679d3bad-155e-4b39-9ff7-7d564f408942,Yes,15453.33,15290.5,113.15,105.22
    679d3bad-155e-4b39-9ff7-7d564f408942,Yes,15453.33,15290.5,113.15,105.22
    679d3bad-155e-4b39-9ff7-7d564f408942,Yes,15453.33,15290.5,113.15,105.22
    679d3bad-155e-4b39-9ff7-7d564f408942,Yes,15453.33,15290.5,113.15,105.22
    679d3bad-155e-4b39-9ff7-7d564f408942,Yes,15453.33,15290.5,113.15,105.22
    679d3bad-155e-4b39-9ff7-7d564f408942,Yes,15453.33,15290.5,113.15,105.22
    679d3bad-155e-4b39-9ff7-7d564f408942,Yes,15453.33,15290.5,113.15,105.22
    

    So, here I did got my answer but instead of printing it once, It is printing 7 times.. How do I fix this.

  • Bhaskar Das
    Bhaskar Das over 4 years
    Your code will generate the output at the CSV with the syntax along in the JSON values.
  • ShivaGaire
    ShivaGaire over 4 years
    Yes @BhaskarDas, It will generate JSON for provided csv
  • vinsent paramanantham
    vinsent paramanantham over 4 years
    writer.writerow(_json[0].keys()) # header row KeyError: 0 getting the above error
  • Night Train
    Night Train almost 4 years
    The json library can also read file objects, without getting the string representation first: json.load(infile)