The Wayback Machine - https://web.archive.org/web/20240914021051/https://www.geeksforgeeks.org/how-to-import-json-data-into-sql-server/
Open In App

How to Import JSON Data into SQL Server?

Last Updated : 15 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:

Import file1.json into SQL server

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;

Convert JSON data

Output:

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


Similar Reads

How to Import and Export SQL Server Data to an Excel File?
SQL Server is very popular in Relational Database and it is used across many software industries. Portability of data is a much-required feature of any database. i.e. Database should support features like exporting database data to Excel/CSV/JSON and also should import data from them. In this article, let us see how to import and export SQL Server
3 min read
Copy tables between databases in SQL Server using Import-and-Export Wizard
Introduction to Import-and-Export Wizard : The wizard allows easy steps through a process, and to execute the data copy process with writing very little or no code. However, for importing and exporting data from one source into another, the below steps could be followed - Open the Object Explorer, select the database, right-click on the database na
2 min read
How to Import and Export SQL Server Database?
Creating and managing a SQL Server database is an essential skill for database administrators and developers. In this article, We will go through the process of setting up a database in SQL Server, from creating the database and tables to inserting records, and finally, exporting and importing the database for backup or transfer purposes. Creating
3 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
SQL Server | Convert tables in T-SQL into XML
In this, we will focus on how tables will be converted in T-SQL into XML in SQL server. And you will be able to understand how you can convert it with the help of command. Let's discuss it one by one. Overview :XML (Extensible Markup Language) is a markup language similar to HTML which was designed to share information between different platforms.
2 min read
How To Import Data from .CSV File With Numeric Values and Texts Into MATLAB Workspace?
A .csv file is a comma-separated file, in which consecutive data columns are separated by commas and the end of a line is the default EOL character. While dealing with small data, it is common to use a .csv file for storing it. In this article, we shall discuss how to import .csv files, with numeric data and their text headers as the column variabl
2 min read
SQL SERVER – Input and Output Parameter For Dynamic SQL
An Input Parameter can influence the subset of rows it returns from a select statement within it. A calling script can get the value of an output parameter. An aggregate function or any computational expression within the stored process can be used to determine the value of the output parameter. A parameter whose value is given into a stored proced
3 min read
SQL - SELECT from Multiple Tables with MS SQL Server
In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we consider table1 contains m rows and table2 contains n
3 min read
How to Execute SQL Server Stored Procedure in SQL Developer?
A stored procedure is a set of (T-SQL ) statements needed in times when we are having the repetitive usage of the same query. When there is a need to use a large query multiple times we can create a stored procedure once and execute the same wherever needed instead of writing the whole query again. In this article let us see how to execute SQL Serv
2 min read
SQL Query to Convert Rows to Columns in SQL Server
In this article we will see, how to convert Rows to Column in SQL Server. In a table where many columns have the have same data for many entries in the table, it is advisable to convert the rows to column. This will help to reduce the table and make the table more readable. For example, Suppose we have a table given below: NAMECOLLEGEROLL NUMBERSUB
2 min read
How to SQL Select from Stored Procedure using SQL Server?
There may be situations in SQL Server where you need to use a stored procedure to get data from a SQL query. For direct data selection from a stored procedure within a query, SQL Server offers options like OPENQUERY and OPENROWSET. The usual way is running the stored procedure independently and then querying the outcomes. The idea of utilizing SQL
3 min read
BULK INSERT in SQL Server(T-SQL command)
BULK INSERT in SQL Server(T-SQL command): In this article, we will cover bulk insert data from csv file using the T-SQL command in the SQL server and the way it is more useful and more convenient to perform such kind of operations. Let's discuss it one by one.  ConditionSometimes there is a scenario when we have to perform bulk insert data from .cs
3 min read
How to Turning a Comma Separated String into Individual Rows in SQL Server?
Given a column in the table having strings with comma-separated values. The task is to split all the comma-separated values and insert them as new rows. So, let's start by creating a database first. Step 1: Create a Database. Query : CREATE DATABASE GFGStep 2: Use the GFG Database. Query : USE GFGStep 3: Create a table Create a table (CommaSeparate
1 min read
Insert Into Select statement in MS SQL Server
Prerequisites - Insert statement in MS SQL Server, Select statement in MS SQL Server Consider a university database. There are two tables namely Student and Marks. In case, the marks of a few students has to be transferred from the marks table to the marks table, there are many ways to do so. One can use subqueries (query nested in another query) b
2 min read
Combine Rows into String in SQL Server
To combine rows into a string in SQL Server, use the SQL COALESCE() function or the SQL CONCAT() function. COALESCE() function in SQL is used to handle null values. It returns non-null values from a row, which can be concatenated into string. CONCAT() function in SQL is used to concatenate two or more strings into a single string. Here we will lear
2 min read
Insert Multiple Values Into Multiple Tables in SQL Server
To insert multiple values into multiple tables in SQL Server, use the OUTPUT clause. The SQL OUTPUT clause allows to insert multiple values into multiple tables in a single query. Output clause clause in SQL is used to capture data affected by Data Manipulation Language (DML) statements like INSERT, UPDATE, or DELETE. SyntaxSyntax to insert multipl
2 min read
Select into and temporary tables in MS SQL Server
In SQL Server, the SELECT INTO TEMP TABLE statement is used to select data from one or more source tables and insert it into a temporary table. Temporary tables are extremely useful when dealing with intermediate results, or when working with subsets of data within a session without modifying or affecting the original tables. In this article, We wi
4 min read
How To Export and Import a .SQL File From Command Line With Options?
Structured Query Language is a computer language that we use to interact with a relational database.SQL is a tool for organizing, managing, and retrieving archived data from a computer database. In this article , we will learn to export and import .SQL files with command line options. Export:You can export the database along with its structure into
2 min read
How to Import a .cer Certificate into a Java KeyStore?
A Java KeyStore is a file that contains certificates. These certificates are used in the Java code. KeyStore and the certificates within it are used to make secure connections from the Java code. The certificates stored can be in several formats. A Java KeyStore is represented by the KeyStore(java.security.KeyStore) class. In this article, we are g
2 min read
How to Import a Word Document into Google Docs
Importing a Word document into Google Docs is a seamless process that enhances your productivity by leveraging the robust features of Google's cloud-based platform. Google Docs allows users to collaborate in real-time, access documents from any device with an internet connection, and utilize a suite of integrated tools that Word alone cannot offer.
5 min read
How to Insert JSON data into MySQL database using PHP?
To insert JSON data into MySQL database using PHP, use the json_decode function in PHP to convert JSON object into an array that can be inserted into the database. Here, we are going to see how to insert JSON data into MySQL database using PHP through the XAMPP server in a step-by-step way. JSON Structure:[{ "data1": "value1", "data2": "value2", .
3 min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
How to Export Data From SQL Server to Flat File in SSIS?
In this article, we will learn how to Export Data From SQL Server To Flat File in SSIS. For this, we are going to create a package to select the data from the SQL server database and export SQL Server table data to the Flat File in the local drive. Prerequisites:Make sure Visual Studio 2019 is installed.Ensure that SQL Server Management Studio is i
3 min read
Numeric and Date-time data types in SQL Server
MS SQL Server supports a wide range of data types. There are a few more important data types that are included in the article. In this article, we will cover numeric SQL server data types and different date-time data types. Let’s discuss one by one. bit : A bit is the smallest unit of a computer system. A bit can be either 0 or 1. The bit datatype
3 min read
Casting value or an expression from one data type to another in SQL server
SQL Server uses the CAST() function to cast or convert a value or an expression from one data type to another. Syntax : CAST ( value AS targettype [ ( length ) ] ) Parameters used : value - value can be any value of any type that will be converted. targettype - targettype is the target data type to which the value will be converted. e.g. INT, BIT,
2 min read
Create a User-Defined Data Type Alias in SQL Server
SQL Server has multiple data types e.g. integers, char, varchar, doubles, strings, etc. which are used for keeping specific values. Although built-in data types could store specific values, sometimes SQL DBA may need to store more specific values and create customized data types. We can create a user-defined data type using below two methods - Usin
2 min read
Different Ways to Know Count of TempDB Data Files in SQL Server
The TempDB database is one of the most significant SQL Server system databases, and it's used to store temporary user objects like user-defined temporary tables, temporary stored procedures, table variables, and indexes, as well as transient stored procedures, table variables, and indexes. To allow transactions to be rolled back, operations in temp
3 min read
How to Export SQL Server Data to a CSV File?
Here we will see, how to export SQL Server Data to CSV file by using the 'Import and Export wizard' of SQL Server Management Studio (SSMS). CSV (Comma-separated values): It is a file that consists of plain text data in which data is separated using comma(,). It is also known as Comma Delimited Files because comma act as a delimiter for CSV files. S
2 min read
How to Select Data Between Two Dates and Times in SQL Server?
In SQL, some transactions need to be extracted based on their completion times and dates. Here, the DATETIME2 data type in SQL to perform such operations. For this article, we will be using the Microsoft SQL Server as our database. Note: Here, we will use the two dates and times given in the query and separate them using the BETWEEN keyword. This i
2 min read
How to Export Data to the .CSV File Using SQL Server Stored Procedure?
CSV (Comma-separated values) is a file that consists of plain text data in which data is separated using comma(,). It is also known as Comma Delimited Files because comma act as a delimiter for CSV files. Here we will see, the process of exporting Data to the .CSV file using the SQL Server Stored Procedure. Step to Export Data to the .CSV File Usin
2 min read