Skip to content

Instantly share code, notes, and snippets.

@jouwdan
Created June 15, 2023 15:36
Show Gist options
  • Save jouwdan/4abd3ba46993a73b5bac14a6a54dfa6f to your computer and use it in GitHub Desktop.
Save jouwdan/4abd3ba46993a73b5bac14a6a54dfa6f to your computer and use it in GitHub Desktop.
Bulk json to csv + add UUID
import json
import os
import csv
import uuid
def read_json_files(folder_path):
json_data = []
for file_name in os.listdir(folder_path):
if file_name.endswith('.json'):
file_path = os.path.join(folder_path, file_name)
with open(file_path, 'r') as file:
data = json.load(file)
json_data.append(data)
return json_data
def json_to_csv(json_data, csv_file):
# Get the header from the first JSON object's keys
header = ['id'] + list(json_data[0].keys())
with open(csv_file, 'w', newline='') as file:
writer = csv.DictWriter(file, fieldnames=header)
writer.writeheader()
for data in json_data:
data_with_id = {'id': str(uuid.uuid4())}
data_with_id.update(data)
writer.writerow(data_with_id)
if __name__ == '__main__':
folder_path = '/your/folder/path/here'
csv_file = 'output.csv'
json_data = read_json_files(folder_path)
json_to_csv(json_data, csv_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment