JSON in SQL Server and Azure SQL: parsing with OPENJSON and JSON_VALUE, producing JSON with FOR JSON, indexing JSON properties, the native json type and JSON index, and when JSON storage is the right design.
SQL Server reads JSON with OPENJSON (shred an array into typed rows using a WITH schema), JSON_VALUE (one scalar) and JSON_QUERY (an object or array), updates it with JSON_MODIFY, and produces it with FOR JSON PATH. To filter on a property quickly, add a computed column on JSON_VALUE and index it. The native json type stores a validated binary form and adds JSON_OBJECTAGG/JSON_ARRAYAGG and the JSON_CONTAINS/JSON index features; it's GA in Azure SQL and shipped with SQL Server 2025, though some features (like the JSON index) are documented as preview.
The design rule: JSON for sparse, variable or pass-through data; real columns for anything you filter, join, constrain or report on.
"SQL Server has had JSON functions since 2016, stored in nvarchar columns, and Azure SQL and SQL Server 2025 now add a native json type. For reading, OPENJSON with a WITH clause is the workhorse: I pass it a document or an array and declare the columns and types I want, and it returns rows I can join or insert. That's how I accept a batch of items from an API in one parameter. JSON_VALUE pulls out a single scalar, JSON_QUERY pulls out a nested object or array, and JSON_MODIFY changes one property. Going the other way, FOR JSON PATH turns a query into JSON, which is handy for returning an order with its lines as one document instead of several queries.
Performance is where people get caught. Filtering with WHERE JSON_VALUE(Data, '$.customer.country') = 'AU' parses every row's document. The fix is a computed column on that JSON_VALUE expression, cast to the right type, with a normal index on it; then it's a seek. The native json type stores a pre-parsed binary form, so reading is cheaper and invalid JSON is rejected on write, and there's a dedicated JSON index aimed at JSON_CONTAINS and path queries, though I'd check its preview status before relying on it.
The bigger question is whether to store JSON at all. I use it for data that's sparse or variable per row, like product attributes that differ between categories, external webhook payloads I need to keep verbatim, or user preferences the database never queries. The mistake I see is putting core fields in a JSON blob to avoid migrations: then you lose types, constraints, foreign keys and statistics, and every query is a parse. If you filter, join, sort, constrain or report on it, it should be a column."
| Function | Does | Returns |
|---|---|---|
ISJSON(x) |
Validity check (use in a CHECK constraint) | 1/0 |
JSON_VALUE(x, path) |
Scalar at path | nvarchar(4000) (2025/Azure: RETURNING type option) |
JSON_QUERY(x, path) |
Object or array at path | JSON fragment |
JSON_MODIFY(x, path, v) |
Set, insert, delete a property | Updated JSON |
OPENJSON(x) WITH (...) |
Shred to typed rows | Rowset |
FOR JSON PATH / AUTO |
Query to JSON | nvarchar(max) |
JSON_OBJECT, JSON_ARRAY |
Build JSON values (2022+) | JSON |
JSON_OBJECTAGG, JSON_ARRAYAGG |
Aggregate rows into JSON | JSON |
| Data | Choice |
|---|---|
| Order status, customer id, totals | Columns (filtered, joined, constrained) |
| Category-specific product attributes | JSON (sparse), with computed columns for the few that are filtered |
| Raw webhook payload for audit | JSON (opaque, kept verbatim) |
| Values reported on by finance | Columns |
A product search filtered on JSON_VALUE(Attributes, '$.brand') over 2.4 million rows: a clustered index scan with 310,000 logical reads and about 1.8 seconds. Adding a persisted computed column Brand AS CAST(JSON_VALUE(Attributes, '$.brand') AS nvarchar(100)) with an index turned it into a seek of 40 reads and 3 ms.
Checks whether the candidate can use SQL Server's JSON features and makes a sound decision about storing JSON.
Green flags
Red flags
nvarchar(max) + ISJSON check native json (Azure SQL, SQL Server 2025)
text, parsed on every read binary, parsed once on write
any size up to 2 GB up to 2 GB, nesting limit 128 levels
works on SQL Server 2016+ validated on write, JSON index support
As of November 2026 the json type is generally available in Azure SQL Database and Azure SQL Managed Instance and is part of SQL Server 2025, which reached GA in November 2025. Microsoft's docs have listed some related pieces, such as CREATE JSON INDEX and the modify method, as preview; check the current status before using them in production.
'$.customer.country' is lax by default: a missing property returns NULL. 'strict $.customer.country' raises an error, which is useful when the property must exist. JSON_VALUE returns NULL (lax) if the value is an object, and truncates at 4,000 characters, so use JSON_QUERY for objects and OPENJSON for long strings.
ALTER TABLE Products ADD Brand AS CAST(JSON_VALUE(Attributes,'$.brand') AS nvarchar(100));
CREATE INDEX IX_Products_Brand ON Products(Brand);
WHERE JSON_VALUE(Attributes,'$.brand') = N'Acme'
-> optimiser matches the expression to the computed column -> Index Seek
The expression must match exactly (including the CAST) for matching to happen; querying the computed column directly is more reliable. For the native type, CREATE JSON INDEX indexes paths for JSON_VALUE, JSON_PATH_EXISTS and JSON_CONTAINS.
A single JSON parameter replaces a table-valued parameter when the caller can't easily build one. OPENJSON with WITH gives typed columns and AS JSON for nested arrays, which you can CROSS APPLY into another OPENJSON.
EF Core maps owned types and complex types to JSON columns (ToJson()), translating LINQ over JSON properties into JSON_VALUE/OPENJSON. EF Core 10 can use the native json type on Azure SQL or SQL Server 2025 when configured for it (for example UseAzureSql or compatibility level 170).
JSON blobs often hide personal information. Under the Privacy Act 1988 and the APPs you still need to know where personal data lives, so document JSON schemas and avoid storing whole third-party payloads with unnecessary personal fields.
JSON_VALUE filter before and after adding an indexed computed column.OPENJSON ... WITH (Lines nvarchar(max) AS JSON) and CROSS APPLY.FOR JSON PATH and compare with two separate queries.json column, insert invalid JSON and observe the error.Accepting a JSON batch, indexing a JSON property, and returning nested JSON.
CREATE TABLE dbo.Products (
ProductId int IDENTITY CONSTRAINT PK_Products PRIMARY KEY,
Name nvarchar(200) NOT NULL,
CategoryId int NOT NULL,
Attributes nvarchar(max) NULL CONSTRAINT CK_Products_Attributes CHECK (ISJSON(Attributes) = 1),
-- On Azure SQL / SQL Server 2025 you could declare: Attributes json NULL
Brand AS CAST(JSON_VALUE(Attributes, '$.brand') AS nvarchar(100)) PERSISTED
);
CREATE INDEX IX_Products_Brand ON dbo.Products (Brand) INCLUDE (Name); -- seekable JSON property
GO
-- Shred a batch sent as one JSON parameter
DECLARE @payload nvarchar(max) = N'[
{"name":"Kettle","categoryId":3,"attributes":{"brand":"Acme","litres":1.7}},
{"name":"Toaster","categoryId":3,"attributes":{"brand":"Acme","slots":4}}
]';
INSERT dbo.Products (Name, CategoryId, Attributes)
SELECT j.Name, j.CategoryId, j.Attributes
FROM OPENJSON(@payload)
WITH (
Name nvarchar(200) '$.name',
CategoryId int '$.categoryId',
Attributes nvarchar(max) '$.attributes' AS JSON
) AS j;
-- Filter uses the indexed computed column
SELECT ProductId, Name FROM dbo.Products WHERE Brand = N'Acme';
-- Update one property
UPDATE dbo.Products
SET Attributes = JSON_MODIFY(Attributes, '$.warrantyYears', 2)
WHERE ProductId = 1;
-- Return orders with lines as one JSON document per customer
SELECT c.CustomerId, c.Name,
(SELECT o.OrderId, o.OrderDate, o.Total
FROM dbo.Orders o
WHERE o.CustomerId = c.CustomerId
FOR JSON PATH) AS Orders
FROM dbo.Customers c
WHERE c.CustomerId = 42
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER;
JSON_VALUE over a large table with no supporting index. Why it is wrong: Every row's document is parsed: a full scan. Instead: Add an indexed computed column (or a JSON index on the native type) for hot properties.JSON_VALUE returns nvarchar(4000). Why it is wrong: Comparisons with numbers or dates convert implicitly and long values become NULL in lax mode. Instead: CAST the result, use OPENJSON ... WITH for typed values, or JSON_QUERY for objects.CHECK (ISJSON(col) = 1) or the native json type.FOR JSON PATH with nested subqueries, or one projection in EF Core.JSON in a relational database is a schema decision, not a convenience. It's excellent at the edges (variable attributes, raw payloads, settings) and harmful at the core. My test: if the business ever asks "show me all X where property Y = Z" at scale, Y becomes a column or at least an indexed computed column.
| Option | Fits when | Cost | Not when |
|---|---|---|---|
| Relational columns | Filtered, joined, constrained data | Migrations for changes | Highly variable shapes |
| nvarchar(max) JSON + ISJSON | Variable data, any SQL Server version | Parse on every read | Hot filters without computed columns |
| Native json type | Azure SQL / SQL Server 2025, frequent reads | Version requirement, some features in preview | Older servers, cross-version code |
| Separate document store (Cosmos DB) | Document-first workloads at scale | Another system, consistency | Data needs joins with relational tables |
Measure with SET STATISTICS IO, TIME ON before and after adding computed columns, and watch Query Store CPU for queries that parse JSON.
Use a JSON column (ComplexProperty(...).ToJson() or OwnsOne/OwnsMany(...).ToJson()) for data that is owned by one row, read and written with it, and rarely queried alone: preferences, a variable set of attributes, a snapshot of an address at order time. Use tables when you need to join, filter or aggregate across rows at scale, enforce foreign keys, unique or check constraints, or update parts concurrently. Performance trade-off: reading the whole document with its owner is cheap (one row, no join), but filtering inside JSON means parsing every document unless you index it. On SQL Server, index a property by adding a computed column over JSON_VALUE(Data, '$.Country') and indexing that column; it works on every supported version. EF Core 10 can map to the native json data type on Azure SQL and SQL Server 2025, which stores documents in a binary form that is faster to read; check your target's support for dedicated JSON indexes before relying on them.
A predicate is SARGable (Search ARGument-able) when the optimiser can turn it into a seek range on an index. The rule: leave the column bare and put any calculation on the other side.
YEAR(OrderDate) = 2026 scans; OrderDate >= '2026-01-01' AND OrderDate < '2027-01-01' seeks.Price * 1.1 > 100 scans; Price > 100 / 1.1 seeks.varchar column vs nvarchar parameter) often scans; match types.LIKE 'abc%' seeks; LIKE '%abc' cannot.ISNULL(col, 0) = 0 scans; col = 0 OR col IS NULL seeks.When the expression is unavoidable, index a persisted computed column. Trade-off: the rewrite is free; the computed column costs storage and writes.
How to tell it (STAR)
**S:** At [company], adding a brand filter to product search made the page slow during business hours. **T:** I had to fix it without redesigning the whole attributes model. **A:** Query Store showed a full scan parsing JSON on every row. I added indexed computed columns for the two attributes people filter on, kept the rest in JSON, and wrote a guideline for future filters. **R:** The query went from about [1.8 seconds and 310,000 reads] to [3 ms and 40 reads], and search p95 returned to [120 ms].
Last updated 23 Nov 2026.