-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdc-json-to-csv.py
More file actions
30 lines (24 loc) · 929 Bytes
/
dc-json-to-csv.py
File metadata and controls
30 lines (24 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import json
import csv
def json_to_csv(json_file, csv_file):
# Open the JSON file
with open(json_file, 'r', encoding='utf-8') as f:
data = json.load(f)
# Extract the "results" array from the JSON data
results = data["results"]
# Open the CSV file in write mode
with open(csv_file, 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
# Write the headers dynamically based on keys present in any object
headers = set()
for item in results:
headers.update(item.keys())
writer.writerow(list(headers))
# Write the data
for item in results:
row = [item.get(key, "") for key in headers]
writer.writerow(row)
# Example usage
json_file = '' # Replace with your JSON file path
csv_file = '' # Replace with your desired CSV file path
json_to_csv(json_file, csv_file)