How To Work with JSON in SQL Server?

Introduction

JSON (JavaScript Object Notation) has become popular as a simple and flexible data format for sending and storing data. The JSON data type and many built-in functions in SQL Server allow you to store and manipulate JSON data. You can use this article to learn how to query, index, and work with JSON documents in SQL Server.

Create one table and work with a table named "members" to store member information, including locations in JSON format

CREATE TABLE Members (
    userID INT PRIMARY KEY,
    FirstName NVARCHAR(50),
    LastName NVARCHAR(50),
	Location NVARCHAR(max),
    Salary NVARCHAR(50)
);

Table


How to insert JSON data into SQL Server?

The JSON data type introduced in SQL Server 2016 and later versions can be used to store JSON data in SQL Server. With the JSON data type, JSON documents can be directly stored in columns, allowing for quick querying and processing.

INSERT INTO Members (userID, FirstName, LastName, Location,Salary)
VALUES (2, 'sahily', 'Mishra', '[{"Type": "Home", "Street": "jankipuram", "City": "lucknow", "Zip": "2032031"}, {"Type": "Work", "Street": "s1", "City": "pune", "Zip": "123920"}]',70000);

How to query JSON data in SQL Server?

Using SQL Server to Query JSON Data: SQL Server has multiple techniques for interacting with JSON data. JSON_VALUE, which extracts a scalar value from a JSON string, is one of the most frequently used functions.

SELECT userID, FirstName, LastName, 
    JSON_VALUE(Location.Value, '$.Street') AS Street,
    JSON_VALUE(Location.Value, '$.City') AS City,
    JSON_VALUE(Location.Value, '$.Zip') AS Zip,Salary
FROM Members
CROSS APPLY OPENJSON(Location) AS Location
WHERE JSON_VALUE(Location.Value, '$.Type') = 'Home';

How to filter data in JSON file?

The JSON_QUERY function allows you to filter JSON data based on set parameters.

SELECT userID, JSON_QUERY(Location) AS Membername
FROM Members;

How to update JSON data in SQL Server?

SQL Server has functions to add, update, and remove properties from JSON documents.

UPDATE Members
SET Location = JSON_MODIFY(Location, '$[2].street', 'New area')
WHERE userID = 2;

How to Aggregate JSON Data in SQL Server?

The FOR JSON clause can aggregate JSON data and return result sets using JSON formatting.

SELECT FirstName, LastName
FROM Members
FOR JSON auto ;


Conclusion

In SQL Server, working with JSON data gives up new opportunities for effectively storing and processing semi-structured data. You may query, index, and manipulate JSON documents within your SQL Server databases without difficulty by using the JSON data type and the JSON methods available.

If you have any queries/suggestions on the article, please leave your questions and thoughts in the comment section below. Follow C# Corner to learn more new and amazing things about SQL Server or to explore more technologies.

Thanks for reading, and I hope you like it.

FAQs

Q 1. What is JSON in SQL Server?

Ans. JSON (JavaScript Object Notation) is a data interchange format that SQL Server supports natively. It allows for storing, querying, and manipulating structured data in a flexible manner.

Q 2. How can I store JSON data in SQL Server?

Ans. You can store JSON data in a column with the JSON data type or generate JSON output from query results using the FOR JSON clause.

Q 3. What functions can I use to query JSON data?

Ans. SQL Server provides functions like JSON_VALUE, JSON_QUERY, and OPENJSON to extract, navigate, and work with JSON data.

Q 4. How do I extract values from a JSON document?

Ans. Use the JSON_VALUE function to extract scalar values from JSON. For example: JSON_VALUE(JsonData, '$.name').

Q 5. Can I work with nested JSON structures?

Ans. Yes, you can navigate through nested JSON structures using dot notation or JSON Path expressions. Use functions like JSON_VALUE and JSON_QUERY for this.

Q 6. How do I modify JSON data in SQL Server?

Ans. You can modify JSON data using the JSON_MODIFY function. It allows updating values, adding new elements, and removing elements from JSON documents.


Similar Articles