
I was working on a data synchronization project, and one of my tasks was to produce a list of possible values for fields from one of the systems. To extract scalar value you can use JSONVALUE function. It always extract JSON object or JSON Array. We can also use the ‘’ argument to get the whole JSON string. We can convert a valid JSON data into a SQL Server rowset using the openjson function, a normal SQL Select statement. It is an optional argument, so we do not get any error. Get only homeaddress.state from the employees table: select jsonvalue (homeaddress,'. Here's how you can query a JSON column in SQL Server. This gives the advantage of storing and querying unstructured data. We can retrieve the whole JSON document by removing the second argument. SQL Server has supported querying JSON columns since the version launched in 2016. You can not extract a scalar value using JSONQUERY function. SELECT JSONQUERY(data, '.employees 0') AS 'Result' It retrieves the first JSON object and gives you the following output.

If the JSON text contains duplicate properties - for example, two keys with the same name on the same level - the JSONVALUE and JSONQUERY functions return only the first value that matches the path. JSONQUERY function introduced in SQL Server 2016. JSON Path Expressions (SQL Server) How built-in functions handle duplicate paths. This done : select Id, PostalCode from Address CROSS APPLY OPENJSON (Address.Data, N'.Addresses') WITH ( TypeCode varchar (50) '.TypeCode', PostalCode varchar (50) '.PostalCode' ) AS AddressesJsonData WHERE TypeCode N'PERMANENT'. SQL JSONQUERY is a JSON function which extracts an object or an array from a JSON string. My salvation came from an unexpected source: Microsoft SQL Server. The interesting part for you it's Analyze JSON data with SQL queries. I started trying to write a regex expression to do it, but quickly decided that one problem was enough. One particular API call was returning mountains of data, and I needed to get a list of distinct values of a particular property buried within the objects of this JSON response.

Have you ever wished that you could query a large JSON document? I wished that very thing recently while working with a 3rd party API.
