How to Import JSON Data into SQL Server?
Last Updated :
15 Jun, 2024
JSON (Javascript Object Notation) is a lightweight text-based data format that is used to interchange data between different systems. It is based on a subset of the Javascript programming language. It also enables us to integrate NoSQL and related concepts into the same database.
You might need to import JSON data into SQL Server if you want to use it for analysis, visualization, or integration with other systems.
To import JSON data into SQL Server, use the OPENROWSET (BULK) function. In this article, we will look at steps to import JSON data in SQL Server.
Steps to Import JSON Data into SQL Server
Follow these easy steps to import JSON data into SQL Server.
Step 1: Use of OPENROWSET (BULK)
OPENROWSET (BULK) is a table-valued function that can read data from any file.. It returns a table with a single column that contains all the contents of the file.
It can just load the entire contents of a file as a text value. (This single large value is known as a single character large object or SINGLE_CLOB.)
Syntax:
SELECT *
FROM OPENROWSET (
BULK ‘file_path’,
SINGLE_CLOB
) AS correlation_name
It reads the content of the file and returns it in BulkColumn. The correlation name must be specified. We have a JSON file named “file1.json”. Content of JSON file:
Query:
[{"Firstname": "ROMY", "Lastname": "KUMARI", "Gender": "female", "AGE" : 22 },
{"Firstname": "PUSHKAR", "Lastname": "JHA", "Gender": "male", "AGE" : 22 },
{"Firstname": "SHALINI", "Lastname": "JHA", "Gender": "female", "AGE" : 21 },
{"Firstname": "SAMBHAVI", "Lastname": "JHA", "Gender": "female", "AGE" : 18 } ]
Step 2: Import file1.json into SQL server
Query:
SELECT *
FROM OPENROWSET (
BULK 'E:\file1.json',
SINGLE_CLOB
) AS import
Output:

The entire content is returned as a single column.
Step 3: Convert JSON data
- Declare a variable.
- Store data of Bulkcolumn in a variable.
- Use OPENJSON() function to convert the JSON output from a variable into a tabular format.
- WITH clause along with the column definition (key should be used as column name).
Query:
DECLARE @JSON VARCHAR(MAX);
SELECT @JSON = BulkColumn
FROM OPENROWSET (
BULK 'E:\file1.json',
SINGLE_CLOB
) AS import;
SELECT *
FROM OPENJSON (@JSON)
WITH (
[Firstname] VARCHAR(20),
[Lastname] VARCHAR(20),
[Gender] VARCHAR(20),
[AGE] INT;

Output:

Advantages of JSON data
- Lightweight data-interchange format.
- It is easy for humans to read and write.
- It is easy for machines to parse and generate.
Functions for JSON Data in SQL Server
Here are some of the built-in functions in SQL Server that help perform specific operations on JSON data.
| Function |
Description |
| ISJSON |
It tests whether a string contains JSON data or not |
| JSON_VALUE |
It extracts a scalar value from a JSON string. |
| JSON_QUERY |
It extracts an object or an array from a JSON string. |
| JSON_MODIFY |
It changes a value in a JSON string. |
- We can parse the JSON text and read or modify the values.
- Transform JSON objects into table format.
- We can run any Transact-SQL query on the converted JSON objects.
- Transform the results of Transact-SQL queries back to JSON format
Please Login to comment...