Advertisement
Beginner SQL Server, JSON Support in SQL Server

What is the difference between the JSON_VALUE and JSON_QUERY functions?

Short answer

JSON_VALUE extracts a single scalar value, like a string or number, from a JSON document, while JSON_QUERY extracts an entire object or array from within the JSON, letting you pull out either a simple individual value or a more complex nested structure depending on which…

Save SQL Server mock interview
Advertisement
AModel answer
JSON_VALUE extracts a single scalar value, like a string or number, from a JSON document, while JSON_QUERY extracts an entire object or array from within the JSON, letting you pull out either a simple individual value or a more complex nested structure depending on which function you use.
SELECT JSON_VALUE(OrderData, '$.customerName') AS CustomerName,
       JSON_QUERY(OrderData, '$.items') AS ItemsArray
FROM Orders;
Real-world example
An order processing system extracts the customer's name as a simple text value using JSON_VALUE, while extracting the entire list of ordered items as a nested JSON array using JSON_QUERY for further processing.
Likely follow-ups
  • What happens if you try to use JSON_VALUE on a JSON object or array instead of a scalar value?
  • Can JSON_QUERY be used to extract a single object from within an array?
Related topics