Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.

Convert JSON to CSV #17

Merged
merged 1 commit into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Convert JSON to CSV
  • Loading branch information
DarkCeptor44 committed Jun 24, 2020
commit bb49d8d83e234ed93d8ffde6139e68ba7b20bcd4
15 changes: 15 additions & 0 deletions projects/Convert_JSON_to_CSV/converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import json

if __name__ == '__main__':
try:
with open('input.json', 'r') as f:
data = json.loads(f.read())

output = ','.join([*data[0]])
for obj in data:
output += f'\n{obj["Name"]},{obj["age"]},{obj["birthyear"]}'

with open('output.csv', 'w') as f:
f.write(output)
except Exception as ex:
print(f'Error: {str(ex)}')
12 changes: 12 additions & 0 deletions projects/Convert_JSON_to_CSV/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"Name": "Akash",
"age": 26,
"birthyear": "1994"
},
{
"Name": "Abhay",
"age": 34,
"birthyear": "1986"
}
]
3 changes: 3 additions & 0 deletions projects/Convert_JSON_to_CSV/output.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Name,age,birthyear
Akash,26,1994
Abhay,34,1986