From ea62a209dce341eddb9c53a3b8865bd29f7c3843 Mon Sep 17 00:00:00 2001 From: Yatharth Date: Mon, 13 May 2024 12:25:42 +0530 Subject: [PATCH] added project Convert_JSON_to_CSV --- .../Convert_JSON_to_CSV/README.md | 20 +++++++++++++++++++ .../Convert_JSON_to_CSV/convert.py | 15 ++++++++++++++ .../Convert_JSON_to_CSV/input.json | 12 +++++++++++ .../Convert_JSON_to_CSV/output.csv | 3 +++ 4 files changed, 50 insertions(+) create mode 100644 contrib/mini-projects/Convert_JSON_to_CSV/README.md create mode 100644 contrib/mini-projects/Convert_JSON_to_CSV/convert.py create mode 100644 contrib/mini-projects/Convert_JSON_to_CSV/input.json create mode 100644 contrib/mini-projects/Convert_JSON_to_CSV/output.csv diff --git a/contrib/mini-projects/Convert_JSON_to_CSV/README.md b/contrib/mini-projects/Convert_JSON_to_CSV/README.md new file mode 100644 index 0000000..a14aeb6 --- /dev/null +++ b/contrib/mini-projects/Convert_JSON_to_CSV/README.md @@ -0,0 +1,20 @@ +# Convert a JSON File into a CSV + +This script takes a JSON file as input and generates a CSV file as output. + +## Prerequisites modules +- `json`: This module is required for handling JSON files. +- Run `pip install json` to install the required external module. + +## How to Run the Script +1. Make sure you have the necessary prerequisites installed. +2. Download or clone the script `converter.py`. +3. Open a terminal or command prompt. +4. Navigate to the directory where `converter.py` is located. +5. Run the script by executing the command `py converter.py`. + +## Additional Information +- The script reads data from the input JSON file and converts it into CSV format. +- It handles nested JSON structures and converts them into flattened CSV rows. +- The output CSV file is created in the same directory as the input JSON file. +- You can customize the behavior of the script by modifying the source code according to your requirements. diff --git a/contrib/mini-projects/Convert_JSON_to_CSV/convert.py b/contrib/mini-projects/Convert_JSON_to_CSV/convert.py new file mode 100644 index 0000000..0e52649 --- /dev/null +++ b/contrib/mini-projects/Convert_JSON_to_CSV/convert.py @@ -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)}') \ No newline at end of file diff --git a/contrib/mini-projects/Convert_JSON_to_CSV/input.json b/contrib/mini-projects/Convert_JSON_to_CSV/input.json new file mode 100644 index 0000000..f943006 --- /dev/null +++ b/contrib/mini-projects/Convert_JSON_to_CSV/input.json @@ -0,0 +1,12 @@ +[ + { + "Name": "Yatharth", + "age": 21, + "birthyear": "2003" + }, + { + "Name": "Sangita", + "age": 53, + "birthyear": "1971" + } + ] \ No newline at end of file diff --git a/contrib/mini-projects/Convert_JSON_to_CSV/output.csv b/contrib/mini-projects/Convert_JSON_to_CSV/output.csv new file mode 100644 index 0000000..d841013 --- /dev/null +++ b/contrib/mini-projects/Convert_JSON_to_CSV/output.csv @@ -0,0 +1,3 @@ +Name,age,birthyear +Yatharth,21,2003 +Sangita,53,1971 \ No newline at end of file