1. Introduction
In today’s API-driven systems, backend databases are no longer just about tables and columns.
APIs often need to send and receive complex hierarchical data, which is naturally represented as JSON or XML.
SQL Server supports both JSON and XML formats natively, allowing developers to easily:
Parse API request data directly into tables.
Return structured responses without heavy transformation in the application layer.
In this article, we’ll explore how to use JSON and XML efficiently inside SQL Server to improve performance, reduce code complexity, and simplify your API integration — especially when using ASP.NET Core or Web API.
2. Why JSON and XML Matter for APIs
Most modern APIs use JSON (JavaScript Object Notation) for data exchange because it’s lightweight and human-readable.
However, many legacy systems and enterprise integrations still rely on XML for structured documents, invoices, and configurations.
SQL Server supports both formats, which means you can:
Store JSON/XML data directly in database columns.
Parse and query nested data.
Return dynamic API responses without looping in C#.
Here’s what it enables in real-world projects:
ASP.NET Core API receives JSON → stored procedure handles JSON directly.
Stored procedure returns JSON → API sends it back without re-serialization.
3. Technical Workflow Flowchart
Below is the technical workflow showing how data moves between an API and SQL Server when using JSON/XML efficiently:
┌─────────────────────────────┐
│ Client (Angular / React) │
│ Sends JSON payload │
└──────────────┬──────────────┘
│
▼
┌──────────────────────────┐
│ ASP.NET Core Web API │
│ Receives JSON/XML input │
└──────────────┬───────────┘
│
▼
┌─────────────────────────────┐
│ SQL Server Stored Procedure│
│ Accepts NVARCHAR(MAX) JSON │
│ Parses using OPENJSON/XML │
└──────────────┬──────────────┘
│
▼
┌──────────────────────────────┐
│ Perform CRUD Operations │
│ using parsed structured data │
└──────────────┬───────────────┘
│
▼
┌────────────────────────────┐
│ Return data as JSON or XML │
│ using FOR JSON / FOR XML │
└────────────────────────────┘
│
▼
┌──────────────────────────┐
│ Web API sends response │
│ back to client │
└──────────────────────────┘
4. Working with JSON in SQL Server
SQL Server (2016 and later) has built-in support for JSON operations.
Let’s start with the most useful functions and patterns.
4.1 Storing JSON Data
You can store JSON as plain text in an NVARCHAR(MAX) column:
CREATE TABLE CustomerOrders (
OrderId INT PRIMARY KEY,
CustomerName NVARCHAR(100),
OrderData NVARCHAR(MAX) -- stores JSON
);
Example JSON data
{"OrderNumber": "ORD123","Items": [
{ "Product": "Keyboard", "Qty": 2, "Price": 450 },
{ "Product": "Mouse", "Qty": 1, "Price": 250 }],"Total": 1150}Insert JSON into the table
INSERT INTO CustomerOrders (OrderId, CustomerName, OrderData)
VALUES (1, 'Rajesh Gami',
N'{
"OrderNumber": "ORD123",
"Items": [
{"Product": "Keyboard", "Qty": 2, "Price": 450},
{"Product": "Mouse", "Qty": 1, "Price": 250}
],
"Total": 1150
}');
4.2 Reading JSON Values
Use JSON_VALUE() to extract a scalar value.
SELECT
JSON_VALUE(OrderData, '$.OrderNumber') AS OrderNumber,
JSON_VALUE(OrderData, '$.Total') AS TotalAmount
FROM CustomerOrders;
Output
OrderNumber | TotalAmount
------------|------------
ORD123 | 1150
4.3 Parsing Arrays with OPENJSON
Use OPENJSON() to split array elements into rows.
SELECT
JSON_VALUE(OrderData, '$.OrderNumber') AS OrderNumber,
Item.value('Product', 'nvarchar(50)') AS ProductName,
Item.value('Qty', 'int') AS Quantity,
Item.value('Price', 'decimal(10,2)') AS UnitPrice
FROM CustomerOrders
CROSS APPLY OPENJSON(OrderData, '$.Items')
WITH (
Product NVARCHAR(50) '$.Product',
Qty INT '$.Qty',
Price DECIMAL(10,2) '$.Price'
) AS Item;
This query expands nested arrays into tabular data — ideal for APIs that send product line items.
4.4 Returning JSON from SQL Server
Instead of letting your ASP.NET Core app serialize the data, you can return it as JSON directly:
SELECT
OrderId,
CustomerName,
OrderData
FROM CustomerOrders
FOR JSON PATH, ROOT('Orders');
Output
{"Orders": [
{
"OrderId": 1,
"CustomerName": "Rajesh Gami",
"OrderData": {
"OrderNumber": "ORD123",
"Items": [
{ "Product": "Keyboard", "Qty": 2, "Price": 450 },
{ "Product": "Mouse", "Qty": 1, "Price": 250 }
],
"Total": 1150
}
}]}This JSON can be sent directly to your API response — saving time and CPU cycles in .NET.
5. Working with XML in SQL Server
XML support in SQL Server has been around since 2005.
It’s still widely used in enterprise-level systems or integrations (e.g., financial, logistics, or government APIs).
5.1 Storing XML Data
CREATE TABLE VendorInvoices (
InvoiceId INT PRIMARY KEY,
VendorName NVARCHAR(100),
InvoiceData XML
);
Example XML
<Invoice>
<Number>INV-999</Number>
<Date>2025-11-05</Date>
<Items>
<Item>
<Name>SSD Drive</Name>
<Qty>2</Qty>
<Price>3200</Price>
</Item>
<Item>
<Name>RAM 16GB</Name>
<Qty>1</Qty>
<Price>5500</Price>
</Item>
</Items>
</Invoice>
Insert XML
INSERT INTO VendorInvoices (InvoiceId, VendorName, InvoiceData)
VALUES (1, 'TechVendor Pvt Ltd',
N'<Invoice>
<Number>INV-999</Number>
<Date>2025-11-05</Date>
<Items>
<Item><Name>SSD Drive</Name><Qty>2</Qty><Price>3200</Price></Item>
<Item><Name>RAM 16GB</Name><Qty>1</Qty><Price>5500</Price></Item>
</Items>
</Invoice>');
5.2 Querying XML with XQuery
You can extract elements using .value() and .nodes().
SELECT
InvoiceData.value('(/Invoice/Number)[1]', 'nvarchar(50)') AS InvoiceNumber,
InvoiceData.value('(/Invoice/Date)[1]', 'date') AS InvoiceDate
FROM VendorInvoices;

Join the conversation! Your thoughts help the community grow.