liveBook · Manning

Json_value

As explained in 100 SQL Server Mistakes and How to Avoid Them

JSON_VALUE is a SQL Server function that extracts a scalar value from a JSON document at a specified path.

Mistake: treating an array like an object. JSON_VALUE() extracts one scalar from a JSON document, but its path must include the array index. Without [0], '$.EmployeeAddress[0].EmployeeID' becomes NULL—even when the array contains one record. section 3.5

Updating an employee address

When address data arrives as JSON, use JSON_VALUE() in the WHERE clause to identify the employee: UPDATE dbo.Employees SET Address = @EmployeeAddress WHERE EmployeeID = JSON_VALUE(@EmployeeAddress, '$.EmployeeAddress[0].EmployeeID'); The path starts with $. for the whole document, then navigates through the EmployeeAddress array. section 3.5

How 100 SQL Server Mistakes and How to Avoid Them covers it

How deep it goes
Explains scalar extraction, root paths, and array indexes, then defers multi-column shredding to OPENJSON().
Examples
Demonstrates extracting EmployeeID from an EmployeeAddress array while updating dbo.Employees.
What it assumes
Assumes familiarity with SQL UPDATE statements and teaches JSON root paths and array indexing explicitly.
What you can do after
Apply it via SQL Server JSON_VALUE() to locate scalar values at explicit JSON paths.
How it connects
Connects JSON_VALUE() with JSON path expressions, FOR JSON PATH, UPDATE, and OPENJSON().

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 SQL Server Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.