File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Expand file tree Collapse file tree 2 files changed +38
-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 )
Original file line number Diff line number Diff line change
1
+ # JSON to CSV
2
+
3
+ This python script converts a JSON file to CSV file
4
+
5
+ Data stored in json format is used for transferring data
6
+ Data in csv format is mostly used for storing / reading data
7
+ This script converts the .json file to .csv file
8
+
9
+
10
+ The default output is 'output.csv'
11
+ > the file path and name can be changed
You can’t perform that action at this time.
0 commit comments