Introduction
In this post, we will learn how to call Zuora API using SSIS. We will go through steps to read data from Zuora and load into SQL Server using SSIS REST API Task and SSIS JSON Source Connector . At the end of this article, you will find a complete sample (dtsx file).
Tasks/Components in SSIS for Consuming RESTful API / WebService
Below are few components you can use to read/write data using REST API for any web service.
JSON Source Connector (Read from REST API, JSON File or OData Service): Use this dataflow component when you have to fetch data from REST API webservice like a table. This component allows you to extract JSON data from webservice and de-normalize nested structure so you can save to Relational database such as SQL Server or any other target (Oracle, FlatFile, Excel, MySQL). This component also supports reading local JSON files or direct JSON string (Wildcard pattern supported too e.g. c:\data\file*.json). | |
Web API Destination Connector (POST data to API URL) : Use this dataflow component when you have to load data into target system via API calls (POST to URL). E.g. Loading Contacts into Marketing System like MailChimp for email campaign. Loading documents into CRM or document storage systems e.g. Couchbase or ElasticSearch. | |
REST API Task : Use this task when you don’t want to pull REST API data in tabular format but want to call rest API for POST data to server, DELETE data from server or things like download HTML page, extract Authentication tokens etc where you not necessarily dealing data in tabular format. This task also allows you many other options such as saving RAW response into variable or file. | |
XML Source Connector (SOAP, File, REST) : Use this dataflow component when you have to fetch data from XML or SOAP webservice and consume data like a table. This component allows you to extract data from webservice and save to SQL Server or any other target (Oracle, FlatFile, Excel, MySQL). This component also supports reading local XML files or direct XML string. |
Screenshot of Complete SSIS Package (Zuora to SQL Server)
Here is screenshot of our full process (we going to outline in next sections)
Prerequisites
Before we perform the steps listed in this article, you will need to make sure the following prerequisites are met:- SSIS designer installed. Sometimes it is referred to as BIDS or SSDT (download it from the Microsoft site).
- Basic knowledge of SSIS package development using Microsoft SQL Server Integration Services.
- Make sure ZappySys SSIS PowerPack is installed (download it, if you haven't already).
- (Optional step). Read this article, if you are planning to deploy packages to a server and schedule their execution later.
Step-By-Step : Using SSIS JSON Source / REST API Task to read Zuora API
Zuora API is somewhat different than other APIs we documented here because Zuora API uses very different Pagination Technique. Some common pagination techniques listed here but it’s not used by Zuora API so we have to do few extra steps.
Let’s look at step by step.
Configure Zuora OAuth Connection
The first step to connect to Zuora API is to configure the OAuth App (Ask your Zuora Admin or Follow Steps listed here). Once you create Oauth App get ClientId and ClientSecret.
Now let’s create new SSIS
- Right click in the Connection Manager Panel > Click New Connection…
- Select ZS-OAUTH from the list
- Configure OAuth Connection as below
- Set OAuth Version OAuth2
- Set OAuth Grant Type to Client Credentials Grant
- Enter Client ID and Client Secret
- For Token URL enter URL https://rest.zuora.com/oauth/token
- Goto Advanced Tab. Select SSL/TLS Protocol v3.0 or Higher
- Click Test (Confirm Green Checkmark)
- Click OK to Save
Configure Variables
Now lets configure few SSIS variables.
Name | Value | Expression | ||
---|---|---|---|---|
url (String) Expression | — blank — |
|
||
bodyString (String) |
|
|||
continue (Boolean) | True | |||
doneFlag (Boolean) | False | |||
queryLocator (String) | — blank — | |||
responseFile (String) | c:\ssis\dump.json | |||
counter (Int32) | 0 |
Configure API Pagination
Now let’s configure For Loop such a way that it keeps looping until @continue flag is True
- Drag For Loop Task from SSIS Toolbox
- Configure as below
- Enter EvalExpression as @continue==true
- Enter AssignExpression as @counter=@counter+1
Configure API Call using SSIS REST API Task
Now let’s configure REST API Task to make a call to get data from Zuora API and save JSON response into File (which we will parse in next step)
So if we making the first request will call API via https://rest.zuora.com/v1/action/query and any call after that (2nd response or Higher) we will call https://rest.zuora.com/v1/action/queryMore . This API uses SQL like query language ZOQL. Click here to learn more about ZOQL
This is controlled by Expression we defined in URL variable.
Configure Data Flow (Read Zuora Data and Load into SQL Server Table)
We will skip this configuration on how to read JSON file and Save to SQL Server. Because it will be covered in next section.
Inside Data Flow when you configure ZS JSON Source
Make sure to set Filter to $.records[*]
Parse Pagination Attributes (done flag, queryLocator for next page)
Once that done you can parse the response from saved File. We need to check for 2 attributes (done and queryLocator ). Parse JSON as below.
Detect Last Page (Set Variables)
Drag two Set Expression Tasks from SSIS Toolbox.
For First Task [Set Continue Flag] Enter Expression as @continue=!@doneFlag
For First Task [Set Query Locator ] Enter Expression as @[User::bodyString]="{ \"queryLocator\" : \"" + @[User::queryLocator] + "\" }"
Save and execute Package
That’s all now you can save package and execute. Check Progress.
Debug Web API call using Fiddler
To test how things looking behind the scene we strongly suggest to use tool like fiddler. You can double click the URL entry (Right side) to see Request and Response Panels. Top panel is Request (URL, Headers, Body) and Bottom Panel is Response.Loading data from REST API to SQL Server
Once you setup HTTP / OAUTH connection we can use JSON Source or XML Source to extract data from API and load into SQL Server. If you are API is XML based API then use XML Source in below examples else use JSON Source. For example purpose we will load data into SQL Server but you can load into any Target (e.g. Flat file, Oracle, Excel) using Microsoft or ZappySys Destination connectors To load REST API data into SQL Server perform the following steps (Screenshots are generic and used for demonstration for purpose, your values can be different than below)- Go to SSIS Data Flow tab
- Drag ZS JSON Source from the SSIS Toolbox (Use ZS XML Source if your API is XML based API)
- Double click to edit Source
- Set API URL you like to call, Check Use Credentials if you need to use HTTP or OAuth connection
- Set Method / Body if its other than GET. For method other than GET make sure you select correct Content-Type from Dropdown (e.g. application/json). This indicate input data format in Body.
- Set HTTP Headers if needed (in Most cases you won't need to set this)
- Select Filter (many times this is not needed). If your API response has array node then select it here. For example if your response looks like this { rows: [ {...}, {...}, {...} ] } then filter would be $.rows[*]. Select Array node you like to extract in the Filter selection UI.
- Configure Pagination if needed on the pagination tab. Check your API documentation see it supports pagination.
- Click OK to Save component settings and generate desired metadata / columns for the output.
- Now drag OLEDB destination from the SSIS toolbox and drop on the Data Flow surface (technically you can use any valid Destination but for demo we will use OLEDB Destination)
- Connect Source to Destination Component
- On OLEDB destination select / create new SQL Connection and then Click "New Table"
- Click on Mappings tab to map columns by names and click OK to save
- Execute the Package
POST / Write Data to API (Insert, Update or Delete)
There will be a time you like to automate POST actions (e.g. create new incident via API call). Check this article to learn more on how to POST , DELETE, PUT data using API callsCommon Errors
There are many error can occur during runtime. Here are most common errors you may face at runtime.The most common error you may face when you run a SSIS package is truncation error. During the design time only 300 rows are scanned from a source (a file or a REST API call response) to detect datatypes but at runtime it is likely you will retrieve far more records. So it is possible that you will get longer strings than initially expected. For detailed instructions how to fix common metadata related errors read an article "How to handle SSIS errors (truncation, metadata issues)".
Another frequent error you may get is authentication error, which happens when you deploy/copy a package to another machine and run it there. Check Deployment to Production below to see why it happens and how to solve this problem.
Deployment to Production
Download Sample SSIS Package (2012 or Higher Version)
Click here to download SSIS Sample Package – ZuoraApiPagination.dtsx
Conclusion. What’s next?
In this article, we have learned how to load data from Zuora and load into SQL Server using SSIS ( drag and drop approach without coding). We used SSIS JSON / REST API Connector to extract data from Zuora REST API using OAuth. JSON Source Connector makes it super simple to parsing complex / large JSON Files or any Web API Response into rows and column so you can load into a database like SQL Server. Download SSIS PowerPack to try many other automation scenarios not discussed in this article.