0

I have a lot of JSON files, I put them in my folder, I want to convert them to CSV format, Should I use import glob? ? I am a novice, how can I modify my code,

#-*-coding:utf-8-*-
import csv
import json
import sys
import codecs

def trans(path):
    jsonData = codecs.open('C:/Users/jeri/Desktop/1', '*.json', 'r', 'utf-8')
    # csvfile = open(path+'.csv', 'w') 
    # csvfile = open(path+'.csv', 'wb') 
    csvfile = open('C:/Users/jeri/Desktop/1.csv', 'w', encoding='utf-8', newline='')
    writer = csv.writer(csvfile, delimiter=',')
    flag = True
    for line in jsonData:
        dic = json.loads(line)
        if flag:
            keys = list(dic.keys())
            print(keys)
            flag = False
        writer.writerow(list(dic.values()))
    jsonData.close()
    csvfile.close()

if __name__ == '__main__':
    path=str(sys.argv[0])
    print(path)
    trans(path)

jeri teri
  • 73
  • 8
  • So, you can loop over the files in a directory using `os.walk` as shown here - https://stackoverflow.com/questions/8625991/use-python-os-walk-to-identify-a-list-of-files. You can define a method as you've done to convert json to csv, and apply the method to every file when you iterate in the loop. – tidakdiinginkan Sep 26 '21 at 03:52

1 Answers1

0

Yes using glob would be a good way to iterate through the .json files in your folder! But glob doesn't have anything to do with the reading/writing of files. After importing glob, you can use it like this:

for curr_file in glob.glob("*.json"):
    # Process each file here

I see that you've used the json module to read in your code snippet. I'd say the better way to go about it is to use pandas.

df = pd.read_json()

I say this because with the pandas library, you can simply convert from .json to .csv using

df.to_csv('file_name.csv')

Combining the three together, it would look like this:

for curr_file in glob.glob("*.json"):
    # Process each file here
    df = pd.read_json(curr_file)
    df.to_csv('file_name.csv')

Also, note that if your json has nested objects, it can't be directly converted to csv, you'll have to settle the organization of data prior to the conversion.