Quantcast
Channel: Welcome To TechBrothersIT
Viewing all articles
Browse latest Browse all 1959

Article 0

$
0
0

 Issue : How to Load Multiple Json Files to SQL Server Table with File Name In SSIS - SSIS Tutorial 2021




In this video you are going to learn how to load Multiple JSON file to SQL Server Table With File Name in SSIS.I have generated Couple of JSON Files online so we can use for our demo.
Below is JSON File Snapshot how its looks like.



Step 1:

Open SQL Server Management Studio (SSMS) and use the script I have provided below, First of all we have to declare the variable which is "Declare @JSON varchar (max), which I have Highlighted in this picture.

Declare @JSON varchar(max)
SELECT @JSON=BulkColumn
FROM OPENROWSET (BULK 'c:\MyJsonFile.json', SINGLE_CLOB) import
insert into dbo.SSISJson
SELECT * FROM OPENJSON @JSON)
WITH
(
 [ID] INT,
 [first_name] varchar(100),
 [last_name] varchar(100),
 [email] varchar(100),
 [gender] varchar(20),
 [ip_address] varchar(10)
)




By using OPENROWSET function we have imported the JSON Files in @JSON variable.




OPENJSON is table value function that can convert JSON data into columns and rows. As you can see below I have use OPENJSON and provided our variable @JSON as input so we can get data in tabular format.



Next step we have to describe the list of columns those we would like to get from our JSON data. If you do not want to read all column data, you can provide the list of only required columns.



If You want To  Load Multiple JSON Files to the table just follow the Steps:

Step 1 :
Create Table By Using Below Command:

create table dbo.SSISJson(
[id] int,
    [first_name] varchar(100),
    last_name] varchar(100)
    [email] varchar(100),
    [gender] nvarchar(20)
    [ip_address] varchar(10),
    filename varchar (1000))


Next : insert data by using below command:
 insert into dbo.SSISJson and Execute




as you can see in below picture 1000 rows affected.


Next Step: Open Visual Studio & Click on Packages + New SSIS Package



In the new SSIS Package we Need Foreach Loop Container :




In Foreach Loop Container we have to select the type &  Path of our JSON Files


Next Click on Variable Mappings & Input Details


Next Execute SQL Task: by Following Below Steps:
Step 1:



Step 2: 


Step 3:


Step 4:


Step 5: Test Connection Succeeded.


Next Click on Expressions Click on the Property & select SQL Statement source then Click on Expression & Copy Paste The Below Given Command in Expression and add the file Path Which is created earlier.
 
Declare @JSON varchar(max)
SELECT @JSON=BulkColumn
FROM OPENROWSET (BULK "++", SINGLE_CLOB) import
insert into dbo.SSISJson
SELECT * FROM '"++"' as filename FROM OPENJSON @JSON
WITH
(
 [ID] INT,
 [first_name] varchar(100),
 [last_name] varchar(100),
 [email] varchar(100),
 [gender] varchar(20),
 [ip_address] varchar(10)
)

Click OK and Go To The SSMS and Read The Data : Select * from dbo.SSISJson and Execute Then Truncate the Table Back To Visual Studio and Execute our Package.



Next Go Back to SSMS And Verify the File 





Video Demo: How to Load Multiple Json Files to SQL Server Table with File Name In SSIS

Viewing all articles
Browse latest Browse all 1959

Trending Articles