How to export REST API to CSV using c# or Python

Introduction to export REST API to CSV

Logo REST APIExport REST API to CSV is in some cases necessary to process the data because many tools can handle CSV files. In this new article, we will show different ways to export the data. The first example will do it using C#. The second example with use Python. Let’s take a look at these examples.

Requirements to export REST API to CSV

  1. First of all, you will need ZappySys ODBC PowerPack installed.
  2. Secondly, you will require Visual Studio installed for the C# example.
  3. Finally, it is necessary Python installed for the Python example.

Export REST API to CSV using C#

C# is a pretty popular programing language. In the first example, we will show how to display the REST API information to a CSV file named. Let’s take a look at the code:

  1. First of all, we will connect to REST API using a connection to the following Data Path:
  2. Secondly, we create a connection using a file stream. We will save the results to a file in the c:\sql\sample.csv:
  3. Also, we will add a query to REST API. Note that with the ZappySys ODBC PowerPack, you can do a simple SQL query to get REST API data. This is pretty simple and intuitive:
  4. In addition, we will read the data and close the connections:
  5. The complete code will be the following:
     
  6. Finally, you will be able to see the file created:
    Export REST API to CSV

    REST API into CSV

Export REST API to CSV using Python

Python is another really popular programming language. The popularity is growing a lot. In this example, we will learn how to Export REST API to CSV using Python.

  1. First of all, you will need to install Pip if not included in Python. Pip is Package Installer.
    For instructions about the installation, refer to this link.
  2. Secondly, you will also need the pyodbc. The pyodbc allows connecting to ODBC using Python. To install it go to the scripts folder of Python where Python is installed and run this command:pip install pyodbc.
  3. Once that pyodbc is installed, we will run the following code:Full Code
  4. Now lets’s understand parts of above code. We have used the csv and pyodbc modules in the code:
  5. Also, we connect to the ZappySys ODBC Driver:
  6. In addition, we have used a cursor to get the rows and send a SQL query to get data from the REST API:
  7. Following code is to open the CSV file stream:
  8. Finally, we will write the data from REST API into the CSV file:
  9. To conclude, if everything is OK, you will be able to see the created CSV file:
    REST API ODBC Python

    Python code REST API

Using ODBC DSN in Connection String

So far we have seen DSN less connection string approach for ODBC Driver but now lets look at another way to use ODBC Driver in your C# or Python code. You can define many settings on DSN Datasource rather than setting in the ConnectionString.

Configure DSN for REST API Connection

  1. First of all, we will access the following URL:
  2. Secondly, in the windows start menu, Search for “ODBC” open the ODBC Data Sources.
  3. Also, in the ODBC Administrator, press Add and select the ZappySys JSON
    Driver:
    REST API ODBC

    Add ODBC JSON

  4. Finally, specify the URL of step 1 and save the configuration:
    REST API

    URL ODBC

Using ODBC DSN in C# Code

Now to use ODBC DSN you created simply change our previous C# Code as below (Just one line)

Using ODBC DSN in C# code to call REST API

Using ODBC DSN in C# code to call REST API

Using ODBC DSN in Python Code

Now to use ODBC DSN you created simply change our previous C# Code as below (Just one line)

 

ZappySys JSON /REST API Driver Query Examples

Reading from XML files or API can be done using the same way as previous sections except you have to use ZappySys XML Driver. Read help file here to see json query examples.

ZappySys XML / SOAP Driver Query Examples

Reading from XML files or API can be done using the same way as previous sections except you have to use ZappySys XML Driver. Read help file here to see xml query examples.

Conclusion

To conclude, in this article, we show how to access REST API using C# and Python. We used the ZappySys ODBC PowerPack that allows accessing to REST API data and JSON files using SQL queries. It is possible to create simple SQL queries and access the data. It is also possible to access to XML files and Web API with this tool. If you liked this tool you can test the ZappySys ODBC PowerPack here.

References

Finally, if you want to read more about this topic, refer to these links:

 

Posted in C# (CSharp), JSON File / REST API Driver, ODBC PowerPack, Python and tagged , , , , , .