2

I have tried a few different ways using Panda to import my JSON to a csv file.

import pandas as pd
df = pd.read_json("CDMP_E2.json")
df.ts_csv("CDMP_Output.csv")

The problem is when I run that code it makes the output all in one "column".

The column header shows up as Credit-NoSQL. Then the data in the column is everything from each "object"

'date':'2021-08-01','type':'CARD','amount':'100'

So it looks like this:

Credit-NoSQL

'date':'2021-08-01','type':'CARD','amount':'100'

I would instead expect to see date, type and amount as the headers instead.

account     date          type     amount     returneddate
ABCD         2021-08-01    CARD    100  
EFGHI        2021-08-01    CARD    150          2021-08-04

My JSON file looks as such:

[
     {
          "Credit-NoSQL":{
               "account":"ABCD"
               "date":"2021-08-01",
               "type":"CARD",
               "amount":"100"
     }
},
{
          "Credit-NoSQL":{
               "account":"EFGHI"
               "date":"2021-08-02",
               "type":"CARD",
               "amount":"150"
               "returneddate":"2021-08-04"
          }
     }
]

so I am not sure if it is the way my JSON file is set up with it's list and such or if I am missing something in my python command. I am new to python and still learning so I am at a loss at what I can do next.

8
  • 1
    When you call read_json() you have to specify how the df is indexed from the JSON. Commented Aug 4, 2021 at 19:17
  • So you want two columns, right? Commented Aug 4, 2021 at 19:19
  • 1
    @PetrL. He said 3 columns: date, type, and amount Commented Aug 4, 2021 at 19:34
  • Sorry @Barmar I am new to python, what does that mean to specify how the df is indexed? Commented Aug 4, 2021 at 19:34
  • You have 2 levels of nested dictionaries. You need to tell it that the rows in the dataframe should be values in the 2nd level, not the 1st level. Also, where does Credit-NoSQL go in the dataframe and CSV? Commented Aug 4, 2021 at 19:36

1 Answer 1

2

No need to use pandas for this.

import json, csv

with open("CDMP_E2.json") as json_file:
    data = [item['Credit-NoSQL'] for item in json.load(json_file)]

# Get the union of all dictionary keys
fieldnames = set()
for row in data:
    fieldnames |= row

with open("CDMP_Output.csv", "w") as csv_file:
    cwrite = csv.DictWriter(csv_file, fieldnames = fieldnames)
    cwrite.writeheader()
    cwrite.writerows(data)
Sign up to request clarification or add additional context in comments.

19 Comments

If I have multiple JSONS with different field names, would I have to create one of these for each one? And list out the fieldname as it appears in the JSON?
I've updated the code to get the field names from the first element of the list.
I just attempted to run this and recieved an error that said json.load is not defined.
Are you sure you put import json, csv at the beginning?
yes I copied exactly how you have it, updating the file name to exaclty what I am using.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.