Queries with JSON data
Using values from JSON in query
Section titled “Using values from JSON in query”JSON_VALUE function enables you to take a data from JSON text on the path specified as the second argument, and use this value in any part of the select query:
select ProductID, Name, Color, Size, Price, JSON_VALUE(Data, '$.Type') as Typefrom Productwhere JSON_VALUE(Data, '$.Type') = 'part'Using JSON values in reports
Section titled “Using JSON values in reports”Once JSON values are extracted from JSON text, you can use them ina any part of the query. You can create some kind of report on JSON data with grouping aggregations, etc:
select JSON_VALUE(Data, '$.Type') as type, AVG( cast(JSON_VALUE(Data, '$.ManufacturingCost') as float) ) as costfrom Productgroup by JSON_VALUE(Data, '$.Type')having JSON_VALUE(Data, '$.Type') is not nullFilter-out bad JSON text from query results
Section titled “Filter-out bad JSON text from query results”If some JSON text might not be properly formatted, you can remove those entries from query using ISJSON function.
select ProductID, Name, Color, Size, Price, JSON_VALUE(Data, '$.Type') as Typefrom Productwhere JSON_VALUE(Data, '$.Type') = 'part'and ISJSON(Data) > 0Update value in JSON column
Section titled “Update value in JSON column”JSON_MODIFY function can be used to update value on some path. You can use this function to modify original value of JSON cell in UPDATE statement:
update Productset Data = JSON_MODIFY(Data, '$.Price', 24.99)where ProductID = 17;JSON_MODIFY function will update or create Price key (if it does not exists). If new value is NULL, the key will be removed. JSON_MODIFY function will treat new value as string (escape special characters, wrap it with double quotes to create proper JSON string). If your new value is JSON fragment, you should wrap it with JSON_QUERY function:
update Productset Data = JSON_MODIFY(Data, '$.tags', JSON_QUERY('["promo","new"]'))where ProductID = 17;JSON_QUERY function without second parameter behaves like a “cast to JSON”. Since the result of JSON_QUERY is valid JSON fragment (object or array), JSON_MODIFY will no escape this value when modifies input JSON.
Append new value into JSON array
Section titled “Append new value into JSON array”JSON_MODIFY function can be used to append new value to some array inside JSON:
update Productset Data = JSON_MODIFY(Data, 'append $.tags', "sales")where ProductID = 17;New value will be appended at the end of the array, or a new array with value [“sales”] will be created. JSON_MODIFY function will treat new value as string (escape special characters, wrap it with double quotes to create proper JSON string). If your new value is JSON fragment, you should wrap it with JSON_QUERY function:
update Productset Data = JSON_MODIFY(Data, 'append $.tags', JSON_QUERY('{"type":"new"}'))where ProductID = 17;JSON_QUERY function without second parameter behaves like a “cast to JSON”. Since the result of JSON_QUERY is valid JSON fragment (object or array), JSON_MODIFY will no escape this value when modifies input JSON.
JOIN table with inner JSON collection
Section titled “JOIN table with inner JSON collection”If you have a “child table” formatted as JSON collection and stored in-row as JSON column, you can unpack this collection, transform it to table and join it with parent row. Instead of the standard JOIN operator, you should use CROSS APPLY. In this example, product parts are formatted as collection of JSON objects in and stored in Data column:
select ProductID, Name, Size, Price, Quantity, PartName, Codefrom Product CROSS APPLY OPENJSON(Data, '$.Parts') WITH (PartName varchar(20), Code varchar(5))Result of the query is equivalent to the join between Product and Part tables.
Finding rows that contain value in the JSON array
Section titled “Finding rows that contain value in the JSON array”In this example, Tags array may contain various keywords like [“promo”, “sales”], so we can open this array and filter values:
select ProductID, Name, Color, Size, Price, Quantityfrom Product CROSS APPLY OPENJSON(Data, '$.Tags')where value = 'sales'OPENJSON will open inner collection of tags and return it as table. Then we can filter results by some value in the table.