JSON API to CSV with PYTHON
Pulling data from an API to use for data analysis may not be the first lesson you learn in dealing with data, but it is a fundamental skill in data analysis. Data Visualization tools now make accessing API data much easier, but those applications may come with some limitations. ETL tools such as Informatica or other analytics tools such as Alteryx offer additional flexibility. Still, eventually, you will run into the need to write a script to access your API data.
I wrote a python script to help pull potential leads and customer data from their online software to support a small consulting business. This company was accessing the data with Power BI, but was looking to build better reporting and ultimately get better insights by aggregating more data. One step in solving their problem was creating a script to automate the data pulls.
This script does the following:
- Uses argpase package to create terminal/command line arguments
- Provides default arguments to pull data for the current month
- Uses calendar package to determine the current month and last day of the month
- Validates that the file parameter for the output ends with “.csv”
- Exits the program if the query fails or no data is found
- The program uses the pandas package to convert the JSON data to a pandas DataFrame and writes it to a CSV file
- Provide a summary of the program execution

How to use the script with command line argument. Important to document usage examples in your script.
.
Script summary output. Output can be suppressed with -v 0 script parameter. This script does not represent how to access all API’s but it provides one example of a solution. A great place to get access API data and to get code examples is Rapid API.
