File tree 4 files changed +50
-0
lines changed
contrib/mini-projects/Convert_JSON_to_CSV
4 files changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Convert a JSON File into a CSV
2
+
3
+ This script takes a JSON file as input and generates a CSV file as output.
4
+
5
+ ## Prerequisites modules
6
+ - ` json ` : This module is required for handling JSON files.
7
+ - Run ` pip install json ` to install the required external module.
8
+
9
+ ## How to Run the Script
10
+ 1 . Make sure you have the necessary prerequisites installed.
11
+ 2 . Download or clone the script ` converter.py ` .
12
+ 3 . Open a terminal or command prompt.
13
+ 4 . Navigate to the directory where ` converter.py ` is located.
14
+ 5 . Run the script by executing the command ` py converter.py ` .
15
+
16
+ ## Additional Information
17
+ - The script reads data from the input JSON file and converts it into CSV format.
18
+ - It handles nested JSON structures and converts them into flattened CSV rows.
19
+ - The output CSV file is created in the same directory as the input JSON file.
20
+ - You can customize the behavior of the script by modifying the source code according to your requirements.
Original file line number Diff line number Diff line change
1
+ import json
2
+
3
+ if __name__ == '__main__' :
4
+ try :
5
+ with open ('input.json' , 'r' ) as f :
6
+ data = json .loads (f .read ())
7
+
8
+ output = ',' .join ([* data [0 ]])
9
+ for obj in data :
10
+ output += f'\n { obj ["Name" ]} ,{ obj ["age" ]} ,{ obj ["birthyear" ]} '
11
+
12
+ with open ('output.csv' , 'w' ) as f :
13
+ f .write (output )
14
+ except Exception as ex :
15
+ print (f'Error: { str (ex )} ' )
Original file line number Diff line number Diff line change
1
+ [
2
+ {
3
+ "Name" : " Yatharth" ,
4
+ "age" : 21 ,
5
+ "birthyear" : " 2003"
6
+ },
7
+ {
8
+ "Name" : " Sangita" ,
9
+ "age" : 53 ,
10
+ "birthyear" : " 1971"
11
+ }
12
+ ]
Original file line number Diff line number Diff line change
1
+ Name,age,birthyear
2
+ Yatharth,21,2003
3
+ Sangita,53,1971
You can’t perform that action at this time.
0 commit comments