Login Register
Code2night
  • Home
  • Guest Posts
  • Blog Archive
  • Tutorial
  • Languages
    • Angular
    • C
    • c#
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  1. Home
  2. Blogpost

How to read json in Sql Server

Date- Aug 10,2022

6007

OpenJson Sql server

JSON in Sql Server

So , you can store json data in any Nvarchar variable in sql server procedure or query. But for reading or deserializing that json data we have to use OpenJson . OpenJson function is provided by sql server for making us able to use json data in normal queries. So , we will have a look at the example below with sample json data.


 DECLARE @JSONData NVARCHAR(MAX)='[
	{
		"color": "red",
		"value": "#f00"
	},
	{
		"color": "green",
		"value": "#0f0"
	},
	{
		"color": "blue",
		"value": "#00f"
	}
	
]'

 SELECT * 
 FROM OPENJSON(@JSONData) WITH (color NVARCHAR(100),value NVARCHAR(100)) 

Here, you can see we have stored json data in a Nvarchar variable and then in the select query we have used OpenJson function. 

In the WITH (color NVARCHAR(100),value NVARCHAR(100))   we are actually telling which properties are there in the json array, you will get those in the select query , we the assigned values to those properties so the output will look like

You can see the result from json in the table format. Sometimes you might now to read json from nested json also , so we will see how to read nested json using OpenJson.

Nested json using OpenJson

So, for reading nested json we can have a look at following example


DECLARE @json NVARCHAR(4000) = N'{  
      "path": {  
            "to":{  
                 "lang":["en-GB", "en-UK","de-AT","es-AR","sr-Cyrl"]  
                 }  
              }  
 }';

SELECT [key], value
FROM OPENJSON(@json,'$.path.to.lang')

Here, we can see we have passed nested json and when we do that we can pass additional parameter to OpenJson to let it know which property we want to read. So the output in this case will be

So, this is how to read json in sql server.

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Setting up database on plesk server
Dec 09, 2023
DataReader in ADO.NET
May 31, 2023
How to rename table column in sql server
Jan 13, 2023
How to find all tables by column name
Jan 13, 2023

Comments

Contents

More in SQL Server

  • Batch Script for Creating Database backups from Sql server 11113 views
  • How to create a read-only MySQL user 10164 views
  • How to Connect to a Database with MySQL Workbench 7530 views
  • How to find all procedures having table reference in Sql ser… 6865 views
  • Converting commas or other delimiters to a Table or List in … 6371 views
View all SQL Server posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
  • Angular
  • C
  • c#
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page