Lesson 1 of 4 JSON in SQL Databases

Storing JSON with the json Data Type

This lesson explains when JSON columns make sense, what the native json type adds, and how computed columns and JSON_MODIFY work with JSON data.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify which attributes belong in JSON and which in relational columns.
  • Explain the advantages of the json type over nvarchar(max).
  • Describe how a computed column over JSON_VALUE allows indexing one property.

When JSON columns make sense

A relational design puts each attribute in its own typed column, which gives constraints, indexes and compact storage. JSON columns suit data that does not fit that shape well: attributes that differ between rows, such as technical specifications that vary by product category; nested structures that are always read and written as a whole, such as a customer's preferences; and payloads kept exactly as received from an API or model. A hybrid design keeps keys, foreign keys and frequently filtered values as columns and the variable remainder as JSON.

Values that are joined, aggregated, constrained or updated individually in most queries belong in columns, because every JSON access must parse the document, and constraints such as FOREIGN KEY cannot reference a property inside JSON.

Sort into groups

Column or JSON?

Exam skills 1.1.3

Decide whether each attribute should be a typed column or part of a JSON document.

Log in to try this activity Interactive activities are free with an account.

The json data type

SQL Server 2025, Azure SQL Database, Azure SQL Managed Instance and SQL database in Fabric provide a native json type. It stores documents in an optimized binary format that is faster to read than parsing text, and it validates documents on insert, so invalid JSON is rejected. With nvarchar(max), which earlier versions used, any text is accepted unless a CHECK (ISJSON(Doc) = 1) constraint is added, and every function call parses the text again.

CREATE TABLE Catalog.Product
(
    ProductID   int IDENTITY PRIMARY KEY,
    Name        nvarchar(200) NOT NULL,
    CategoryID  int NOT NULL,
    ListPrice   decimal(10,2) NOT NULL,
    Specs       json NULL,                     -- varies by category
    WeightKg AS JSON_VALUE(Specs, '$.weightKg' RETURNING decimal(6,2))   -- computed column
);

CREATE INDEX IX_Product_WeightKg ON Catalog.Product (WeightKg);

A computed column over JSON_VALUE exposes one property as a regular column, which can then be indexed with an ordinary B-tree index and used in constraints. This is the classic way to make a frequently filtered JSON property fast.

Scenario questions

Making JSON properties fast

Exam skills 1.1.3

Choose the design that fits each requirement.

Log in to try this activity Interactive activities are free with an account.

Modifying JSON

JSON_MODIFY(doc, '$.path', value) returns a document with one property set, inserted or removed (when the value is NULL in lax mode), and append $.array adds an element to an array. An UPDATE that sets the column to JSON_MODIFY changes one property without rewriting the application's whole object. Like any column, a json column can be NULL, and an empty object {} is not the same as NULL.

UPDATE Catalog.Product
SET Specs = JSON_MODIFY(Specs, '$.waterproofRating', 'IPX7')
WHERE ProductID = 42;
Match the pairs

JSON_MODIFY

Exam skills 1.1.3 · 1.3.3

Match each JSON_MODIFY call with its effect.

Log in to try this activity Interactive activities are free with an account.
Worked example

Product specifications

A retailer sells tents, boots and stoves. Each category has 10 to 30 specifications that change as suppliers add new ones, and the website shows all of them on the product page. Search filters use only the product weight, which every product has. How should the developer store the specifications?

Store the specifications in a json column, and expose the weight through a computed column over JSON_VALUE(Specs, '$.weightKg' RETURNING decimal(6,2)) with a B-tree index (or store weight as an ordinary column). The json type validates documents and avoids a schema change for every new specification, while the one filtered property stays fast. A column per specification would need constant schema changes and leave most columns NULL, and nvarchar(max) would accept invalid JSON unless a CHECK constraint is added.

Check yourself

What does nvarchar(max) need in order to reject invalid JSON?

Show answer

A CHECK constraint such as ISJSON(col) = 1; the json data type validates documents automatically.

Exam Tips

Remember for the exam

  • Varying attributes read as a whole: a json column; filtered, joined or constrained values: real columns.
  • The json type validates documents and stores them in a binary format.
  • Index one JSON property: a computed column over JSON_VALUE with a B-tree index.
  • Change one property: JSON_MODIFY.