Introduction
SQL Server 2012 and all previous versions lacking native JSON support, same theory is true for their ETL Platform SSIS. If you are one of them who wants to export JSON from SQL Server or any other RDBMS like MySQL, Oracle then you can easily output complex JSON using any of the following components.
Five ways of creating JSON inside SSIS
ZappySys toolset provides multiple ways creating JSON inside SSIS. You can choose method suitable for your scenario. Each method has pros and cons so choose it as per your need.
JSON File Destination
SSIS JSON Destination can be used to generate simple or complex JSON files out of relational data source such as SQL Server, Oracle, MySQL. In few clicks you can create desired JSON Shape from single or multiple tables (Parent/Child Nested Documents).
PROS: Multiple data flow components can be attached. Which means unlike Export JSON Task you are not limited to OLEDB or ODBC Sources only.
CONS: When you remove upstream component it may break Dataset mappings. Also server side Lookup Option is not available. File Splitting Options not available.
Here are some articles for use case of this component.
JSON Generator Transform
SSIS JSON Generator Transform can be used to generate single or multiple JSON documents from any type of data sources (e.g. SQL Server, MySQL, Flat File, Excel) inside data flow task. It takes multiple inputs and outputs JSON String Output Column which can be used to feed JSON to other downstream system.
PROS: Multiple data flow components can be attached. Which means unlike Export JSON Task you are not limited to OLEDB or ODBC Sources only.
CONS: When you remove upstream component it may break Dataset mappings. Also server side Lookup Option is not available. File Splitting Options not available. Output is JSON String Column.
Here are some articles for use case of this transform.
Export JSON Task
SSIS Export JSON File Task can be used to generate simple or complex JSON files out of relational data source such as SQL Server, Oracle, MySQL. It also support exporting directly to cloud (e.g. Azure Blob Storage, Amazon AWS S3, Secure FTP). In few clicks you can create desired JSON Shape from single or multiple tables (Parent/Child Nested Documents)
PROS: Simple to use with flexible options to export to file, variable, AWS S3, Azure Blob, FTP / SFTP location. Allows to split records. File Splitting Options available (Split By Size / Row Count).
CONS: You can only use limited data sources as Dataset input (e.g. OLEDB / ODBC or SQL Server).
Here are some articles for use case of this task.
Template Transform
Template Transform can be used to create dynamic strings inside data flow. You can use upstream columns as placeholders or use SSIS variables anywhere in the template to create desired text (e.g. XML / JSON document).
PROS: Very Simple to use.
CONS: Cannot use multiple inputs. It may not be used to create Complex JSON and options like do not output NULL attributes is not possible.
Here are some articles for use case of this transform.
Logging Task (Control Flow Approach)
Just like Template Transform you can use ZS Logging Task to create JSON string from Template and Variable Placeholders.
Here is how to use Logging Task to save JSON into a variable.
- Select Variable which you like to Set on Logging Task
- Enter your Template JSON String which you like to produce (see example below). Notice the use of yyyy-MM-dd format specifier and also JSONENCODE function.
12345{ActionId: {{User::vActionId}},CreatedOn: "{{User::vOrderDate,yyyy-MM-ddTHH:mm:ss}}",Notes: "{{User::vNotes,JSONENCODE}}"} - Select Message Type = None
- Uncheck Append, and Prefix options like below
Here are some articles for use case of this task.
Create JSON using SSIS Export JSON Task
Now let’s look at Export JSON Task form example to create JSON. Steps listed in below sections are very similar to JSON File Destination or JSON Generator Transform.
ZappySys offers innovative Layout Editor for JSON / XML creation from multiple sources. You can use Layout Editor to produce desired JSON Layout (Layout is stored in XML format behind the scene) and then in later step you will see how to make it dynamic so values are changed at runtime.
You can read layout as direct value, read from variable or read from file from disk.
Select source connection for data
First lets create new OLEDB or ADO.net connection from where we will read data. In our case we reading from Northwind Database.
Create Datasets
Now add datasets which will be used to produce JSON File. One dataset will be always Root Dataset (Example: If your Root information is about customer then select customer table or query as your Main dataset. All other sections will be child sections)
Add Output Columns for Main JSON Document
Now its time to add columns for our output. If * is added by default then remove. * means output all columns. When you add columns first make sure you select correct parent node under which you want to add columns. In our case “Mappings” is parent node.
Now let’s look at how to add JSON Array. For example Customer may have one or more Orders so we need to add node called Orders which may Produce JSON fragment like below
Orders : [ {...} , {...} ]Once customer related columns added, its time to add Orders section.
- This is Array of documents so first highlight “Mappings” node
- Click “Add Document Array” option from toolbar.
- On Add/Edit Elements UI specify name for JSON Attribute and specify correct relationship to parent dataset. In our case Orders related to Customers using CustomerID key.
Handling JSON Array with dummy root dataset (Multiple Arrays)
So in previous section we saw how to add array and define JOIN condition with parent dataset. Now what if you really have no parent dataset to join with ? For example we need to produce following sample JSON where Order array is related to Customer by common Column CustomerID as JOIN column. But Customer is not bound with any parent column. So how do you define JOIN condition? In such case you have to use below technique called dummy root dataset. (See next section on how to handle only one dataset using Single Dataset Array Mode)
Sample JSON (Dummy root pattern)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
{ "Customers": [ { "CustomerID": "ALFKI", "CompanyName": "Alfreds Futterkiste", "Orders": [ { "OrderID": 1000, "OrderDate": "2015-12-01" }, { "OrderID": 1001, "OrderDate": "2015-12-01" } ] }, { "CustomerID": "ANATR", "CompanyName": "Ana Trujillo Emparedados y helados", "Orders": [ { "OrderID": 1002, "OrderDate": "2015-12-01" }, { "OrderID": 1003, "OrderDate": "2015-12-01" } ] } ] } } |
Since root level always needs dataset, we must provide dummy dataset (only one row) even though there is no need for Parent Dataset JOIN in Customer Array. Typically you can write query like below to product dummy row in SQL Server or other database using OLEDB Source or Use Dummy Data Source with Max Rows=1 setting to produce one fake record.
1 |
SELECT 1 as Col1 |
Using Dummy DataSource as Input
For generating JSON which needs dummy root. Perform the following steps
- Drag Dummy Data Source from SSIS Toolbox. Change How many rows to 1. Click OK
- Now drag two more OLEDB sources and Configure (e.g. Customers and Orders)
- Now drag ZS JSON FIle Destination component or ZS JSON Generator
- Now connect Dummy datasource then Customers and then Orders
- Double click JSON component to configure layout. You may notice that Dummy Input is set as Root by default.
- You can now select Mappings node and click on Add Array Documents icon. Configure like below. Notice we have not selected any JOIN criteria because its joined with Dummy Root.
- Now select newly added node and Click Add column icon to add CustomerID, CompanyName column under Customers array
- Now select Customers array node and click Array Documents icon to Add Orders array.
- Once Orders Array ia added you can Select Orders node and Insert new element to add OrderID and OrderDate columns
- Finally it will look like below
Single dataset used as array
Now lets look at one more pattern just like described in the previous section but now we have just one nested level (Only customers and not using Orders). If you have such case then choose Single dataset pattern option in the output mode. On Export Task you may see as check box rather than dropdown. To produced desired JSON (see blow) perform these steps.
- Select Output mode as Single Dataset Array
- In your Layout builder click on Add Document Array Icon and name it “Customers”.
- After that highlight newly added node > Right-Click > Add Elements under that node
- That’s it you will now see preview like below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
{ "Customers": [ { "CustomerID": "ALFKI", "CompanyName": "Alfreds Futterkiste" }, { "CustomerID": "ANATR", "CompanyName": "Ana Trujillo Emparedados y helados" } ] } } |
How to export JSON in 2D Array format
If you have to export JSON as 2D array format then you can change following option.
Here are examples of output for each option.
Select say you have source data with 2 columns id and name.
Default
1 |
[{id:1,name:"AA"}, {id:2,name:"BB"}] |
Multicontent
1 |
{id:1,name:"AA"}{id:2,name:"BB"} |
ArrayDocs
1 |
[{id:1,name:"AA"}, {id:2,name:"BB"}] |
Array2D
1 |
[[1,"AA"],[2,"BB"]] |
Array2DWithHeader
1 |
[["id","name"],[1,"AA"],[2,"BB"]] |
ArrayLines
1 |
[1,"AA"][2,"BB"] |
ArrayLinesWithHeader
1 |
["id","name"][1,"AA"][2,"BB"] |
Preview JSON output using live preview
You can enable / disable Live preview of JSON layout as below.
Make things dynamic (Using SSIS Variable for SQL or Layout)
There will be a time when you have use dynamic values in your JSON generation. You may have to Alias something from variable or you have to supply Variable in your SQL used for Dataset. Not to worry. ZappySys Supports multiple ways to make it dynamic. You can use SSIS Variables following 3 ways to make Layout XML dynamic.
- Use SSIS variable inside Dataset SQL (Only for ZS Export JSON Task)
- Anywhere inside Layout XML (Click Edit Code option)
- Load Layout From Variable (Indirect Method)
Using Variable inside Dataset SQL
Here is how you can use Variable inside dataset SQL for ZS Export JSON Task
Using Variable inside Layout XML
Here is how you can insert SSIS Variable anywhere in Layout XML. In below Example we are choosing Alias for one column from some SSIS Variable.
Load Layout From Variable
If you have some need to load Layout XML from File or SSIS Variable. You can use Load Layout from Variable Option.
For example in below screenshot we are creating layout first and ave to some variable. Or you can load from external file using ZS Advanced File System Task. Connect Logging task to Export JSON Task and you ready to produce JSON.
Here is how to load Layout From any SSIS Variable. You can also load Layout from XML file or Variable which contains layout File Path.
Other JSON Output Options
Execute package and verify generated JSON it should look like below (Date format may be different in your case if you have set different on JSON options tab). For clarity we have formatted JSON below but in your output it may be compact version so reduce file size.
Indent JSON Output
You can check Indent option to make JSON look pretty like below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
{ "CustomerID": "ALFKI", "CompanyName": "Alfreds Futterkiste", "ContactName": null, "Orders": [ { "OrderID": 10643, "OrderDate": "1997-08-25T00:00:00" }, { "OrderID": 10692, "OrderDate": "1997-10-03T00:00:00" }, { "OrderID": 10702, "OrderDate": "1997-10-13T00:00:00" }, { "OrderID": 10835, "OrderDate": "1998-01-15T00:00:00" }, { "OrderID": 10952, "OrderDate": "1998-03-16T00:00:00" }, { "OrderID": 11011, "OrderDate": "1998-04-09T00:00:00" } ] } |
Hide NULL Attribute Option
There will be a time when you want to Hide NULL attribute rather than showing MyAttr: null this is also possible by checking Hide NULL Attribute Option on Options Tab.
Export JSON to Amazon S3, Azure Blob, Secure FTP (SFTP / FTPS)
Export JSON Task support some really good options to save file to other than local file system. If you want to export JSON directly to Amazon / Azure Cloud or FTP Server without saving to local disk then you can use Azure / FTP / Amazon Connection Manager on Target Tab of Export JSON Task. This option is only available for PRO Edition Users.
Conclusion
If you want to output JSON from SQL Server or any other relational DB then its not simple task in SSIS specially when you have nested JSON documents (e.g. Customer->Orders). In this post you have seen how to use SSIS JSON Export Task to generate nested JSON from multiple datasets.