JSON is a popular textual data format used for exchanging data in modern web and mobile applications. JSON is also used for storing unstructured data in log files or NoSQL databases like Microsoft Azure DocumentDB. Many REST web services return results formatted as JSON text or accept data formatted as JSON. For example, most Azure services such as Azure Search, Azure Storage, and Azure DocumentDb have REST endpoints that return or consume JSON. JSON is also the main format for exchanging data between web pages and web servers using AJAX calls.
Here’s an example of JSON text:
"skills": ["SQL", "C#", "Azure"]
SQL Server provides built-in functions and operators that let you do the following things with JSON text.
- Parse JSON text and read or modify values.
- Transform arrays of JSON objects into table format.
- Run any Transact-SQL query on the converted JSON objects.
- Format the results of Transact-SQL queries in JSON format.
Key JSON capabilities of SQL Server
Here’s more info about the key capabilities that SQL Server provides with its built-in JSON support.
Extract values from JSON text and use them in queries
If you have JSON text that’s stored in database tables, you can use built-in functions to read or modify values in the JSON text.
- Use the JSON_VALUE function to extract a scalar value from a JSON string.
- Use JSON_QUERY to extract an object or an array from a JSON string.
- Use the ISJSON function to test whether a string contains valid JSON.
- Use the JSON_MODIFY function to change a value in a JSON string.
In the following example, the query uses both relational and JSON data (stored in the jsonCol column) from a table:
For more info, see Validate, Query, and Change JSON Data with Built-in Functions (SQL Server), JSON_VALUE (Transact-SQL), and JSON_QUERY (Transact-SQL).
Change JSON values
If you have to modify parts of JSON text, you can use the JSON_MODIFY function to update the value of a property in a JSON string and return the updated JSON string. The following example updates the value of a property in a variable that contains JSON.
Convert JSON collections to a rowset
You don’t need a custom query language to query JSON in SQL Server. To query JSON data, you can use standard T-SQL. If you have to create a query or report on JSON data, you can easily convert JSON data to rows and columns by calling the OPENJSON rowset function. For more info, see Convert JSON Data to Rows and Columns with OPENJSON (SQL Server).
The following example calls OPENJSON and transforms the array of objects stored in the
@json variable to a rowset that can be queried with a standard SQL SELECT statement:
OPENJSON transforms the array of JSON objects into a table in which each object is represented as one row, and key/value pairs are returned as cells. The output observes the following rules.
- OPENJSON converts JSON values to the types specified in the WITH clause.
- OPENJSON can handle both flat key/value pairs and nested, hierarchically organized objects.
- You don’t have to return all the fields contained in the JSON text.
- OPENJSON returns NULL values if JSON values don’t exist.
- You can optionally specify a path after the type specification to reference a nested property or to reference a property by a different name.
- The optional strict prefix in the path specifies that values for the specified properties must exist in the JSON text.
For more info, see Convert JSON Data to Rows and Columns with OPENJSON (SQL Server) and OPENJSON (Transact-SQL).
Convert SQL Server data to JSON or export JSON
Format SQL Server data or the results of SQL queries as JSON by adding the FOR JSON clause to a SELECT statement. Use FOR JSON to delegate the formatting of JSON output from your client applications to SQL Server. For more info, see Format Query Results as JSON with FOR JSON (SQL Server).
The following example uses PATH mode with the FOR JSON clause.
The FOR JSON clause formats SQL results as JSON text that can be provided to any app that understands JSON. The PATH option uses dot-separated aliases in the SELECT clause to nest objects in the query results.
For more info, see Format Query Results as JSON with FOR JSON (SQL Server) and FOR Clause (Transact-SQL).
Combine relational and JSON data
SQL Server provides a hybrid model for storing and processing both relational and JSON data using standard Transact-SQL language. You can organize collections of your JSON documents in tables, establish relationships between them, combine strongly-typed scalar columns stored in tables with flexible key/value pairs stored in JSON columns, and query both scalar and JSON values in one or more tables using full Transact-SQL.
JSON text is typically stored in varchar or nvarchar columns and is indexed as plain text. Any SQL Server feature or component that supports text supports JSON, so there are almost no constraints on interaction between JSON and other SQL Server features. You can store JSON in In-memory or Temporal tables, you can apply Row-Level Security predicates on JSON text, and so on.
If you have pure JSON workloads where you want to use a query language that’s customized for the processing of JSON documents, consider Microsoft Azure DocumentDB.
Here are some use cases that show how you can use the built-in JSON support in SQL Server.
Return data from a SQL Server table formatted as JSON
For example, you might want to generate JSON output that’s compliant with the OData specification. The web service expects a request and response in the following format.
This OData URL represents a request for the ProductID and ProductName columns for the product with id 1. You can use FOR JSON to format the output as expected in SQL Server.
The output of this query is JSON text that’s fully compliant with OData spec. Formatting and escaping are handled by SQL Server. SQL Server can also format query results in any format such as OData JSON or GeoJSON – for more info, see Returning spatial data in GeoJSON format.
Analyze JSON data with SQL queries
If you have to filter or aggregate JSON data for reporting purposes, you can use OPENJSON to transform JSON to relational format. Then use standard Transact-SQL and built-in functions to prepare the reports.
Both standard table columns and values from JSON text can be used in the same query. You can add indexes on the
JSON_VALUE(Tab.json, '$.Status') expression to improve performance of query. For more info, see Index JSON data.
Import JSON data into SQL Server tables
If you have to load JSON data from an external service into SQL Server, you can use OPENJSON to import the data into SQL Server instead of parsing the data in the application layer.
Load JSON files into SQL Server
Information stored in files can be formatted as standard JSON or Line-Delimited JSON. SQL Server can import the contents of JSON files, parse it using the OPENJSON or JSON_VALUE functions, and load it into tables.
- If your JSON documents are stored in local files, on shared network drives, or in Azure File Storage locations that can be accessed by SQL Server, you can use bulk import to load your JSON data into SQL Server. For more info about this scenario, see Importing JSON files into SQL Server using OPENROWSET (BULK).
- If your line-delimited JSON files are stored in Azure Blob Storage or the Hadoop file system, you can use Polybase to load JSON text, parse it in Transact-SQL code, and load it into tables.
Test drive built-in JSON support
Test drive built-in JSON support with the AdventureWorks sample database. To get the AdventureWorks sample database, download at least the database file and the samples and scripts file from here. After you restore the sample database to an instance of SQL Server 2016, unzip the samples file and open the “JSON Sample Queries procedures views and indexes.sql” file from the JSON folder. Run the scripts in this file to reformat some existing data as JSON data, run sample queries and reports over the JSON data, index the JSON data, and import and export JSON.
Here’s what you can do with the scripts included in the file.
- Denormalize the existing schema to create columns of JSON data.
- Store information from SalesReasons, SalesOrderDetails, SalesPerson, Customer, and other tables that contain information related to sales order into JSON columns in the SalesOrder_json table.
- Store information from EmailAddresses/PersonPhone tables into the Person_json table as arrays of JSON objects.
- Create procedures and views that query JSON data.
- Index JSON data – create indexes on JSON properties and full-text indexes.
- Import and export JSON – create and run procedures that export the content of the Person and the SalesOrder tables as JSON results, and import and update the Person and the SalesOrder tables using JSON input.
- Run query examples – run some queries that call the stored procedures and views created in steps 2 and 4.
- Clean up scripts – don’t run this part if you want to keep the stored procedures and views created in steps 2 and 4.
Learn more about built-in JSON support
Topics in this section
Format Query Results as JSON with FOR JSON (SQL Server)
Use the FOR JSON clause to delegate the formatting of JSON output from your client applications to SQL Server.
Convert JSON Data to Rows and Columns with OPENJSON (SQL Server)
Use OPENJSON to import JSON data into SQL Server, or to convert JSON data into relational format for an app or service that can’t currently consume JSON directly, such as SQL Server Integration Services.
Validate, Query, and Change JSON Data with Built-in Functions (SQL Server)
Use these built-in functions to validate JSON text and to extract a scalar value, an object, or an array.
JSON Path Expressions (SQL Server)
Use a path expression to specify the JSON text that you want to use.
Index JSON data
Use computed columns to create collation-aware indexes over properties in JSON documents.
Solve common issues with JSON in SQL Server
Find answers to some common questions about the built-in JSON support in SQL Server.
Microsoft blog posts