File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed
Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 1+ ## json and csv modules are installed by default, no need to install
2+
3+ import json
4+ import csv
5+
6+
7+ ## specify file path , along with extension
8+ input_json_file = 'input.json'
9+ csv_file = 'output.csv'
10+
11+ # Load JSON data from a file
12+ with open (input_json_file ) as json_file :
13+ data = json .load (json_file )
14+
15+ # Open the CSV file in write mode
16+ with open (csv_file , 'w' , newline = '' ) as csvfile :
17+ # Create a CSV writer
18+ writer = csv .writer (csvfile )
19+
20+ # Write the header row based on the keys of the first JSON object
21+ writer .writerow (data [0 ].keys ())
22+
23+ # Write the data rows
24+ for item in data :
25+ writer .writerow (item .values ())
26+
27+ printf ("Conversion successfull. The output file is saved as : " + csv_file )
You can’t perform that action at this time.
0 commit comments