Skip to content

Commit ea62a20

Browse files
committed
added project Convert_JSON_to_CSV
1 parent 0855a5e commit ea62a20

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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)}')
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"Name": "Yatharth",
4+
"age": 21,
5+
"birthyear": "2003"
6+
},
7+
{
8+
"Name": "Sangita",
9+
"age": 53,
10+
"birthyear": "1971"
11+
}
12+
]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Name,age,birthyear
2+
Yatharth,21,2003
3+
Sangita,53,1971

0 commit comments

Comments
 (0)