This Python program fetches JSON data from a given URL and converts it into either a CSV or XLSX file format. The program provides a prompt to enter the data link, file type, and file name, and returns the converted file with the specified data.
Note: While this program is intended to be general and work with any JSON link, it has been thoroughly tested and works best with the following two data sources:
While there are libraries available, such as pandas
, that provide convenient methods for converting JSON to CSV/XLSX, this program was designed to demonstrate a manual approach for educational purposes. By implementing the conversion process without relying on a library, it helps users understand the underlying logic and steps involved in the conversion.
By writing the conversion code manually, it allows users to have more control over the process and customize it as needed. Additionally, it reduces dependencies on external libraries, making the program more lightweight and easier to understand and maintain.
However, it's important to note that using a library like pandas
can offer more robust and efficient solutions for handling JSON data conversion. If you prefer a more streamlined approach, it's recommended to explore available libraries that provide specific functionality tailored to JSON conversion.
Note: As it obsered libraries like pandas are not able to go much deeper in data by its own and resulted in lessure number of features as compared to my solution, so for more control custom codes can be preferred for larger and complex datasets
E.g.
For the https://data.nasa.gov/resource/y77d-th95.json data pandas .read_json()
resulted in 12 features:
'name', 'id', 'nametype', 'recclass', 'mass', 'fall', 'year', 'reclat','reclong','geolocation', ':@computed_region_cbhk_fwbd',':@computed_region_nnqa_25f4'
and my code resulted in 14 features:
'id',':@computed_region_cbhk_fwbd','name',':@computed_region_nnqa_25f4','fall','geolocation_coordinates_1','geolocation_coordinates_2','geolocation_type','mass','nametype','recclass','reclat','reclong','year'
- Clone the repository or download the source code files.
- Make sure you have Python 3.x installed on your system.
- Install the required dependencies by running the following command:
pip install -r requirements.txt
- Run the Python script using the command:
python main.py
- Enter the data link when prompted. Ensure that the URL contains valid JSON data.
- Choose the desired file type by entering 1 for 'csv' or 2 for 'xlsx' when prompted.
- Provide a file name (without the file extension) for the converted file.
- The program will convert the JSON data and save it as a CSV or XLSX file in the current directory.
Here's an example usage of the program:
enter url
https://data.nasa.gov/resource/y77d-th95.json
Enter
1 for json to .csv
2 for json to .xlsx
1
Enter file name without .format
nasa_meteroite_converted_json_csv
- Ensure that the provided URL contains valid JSON data; otherwise, the program may not function correctly.
- The program uses the requests library to fetch JSON data from the URL and the pandas library to convert and save the data as CSV or XLSX.
- The program requires an active internet connection to fetch the JSON data.
- Please make sure you have the necessary permissions to write files to the current directory.
Contributions to the project are welcome! If you have any suggestions, bug reports, or feature requests, please open an issue or submit a pull request.
This project is licensed under the MIT License.