| Start | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Format Query Results as JSON with FOR JSON Input table data (People table)
Query Result |
||||||||||||||||||
| Parse JSON text JSON_VALUE and JSON_QUERY functions parse JSON text and return scalar values or objects/arrays on the path in JSON text. Result
|
||||||||||||||||||
| Join parent and child JSON entities using CROSS APPLY OPENJSON Join parent objects with their child entities, for example we want a relational table of each person and their hobbies Query Alternatively this query can be written using the WITH clause. Result
|
||||||||||||||||||
| Index on JSON properties by using computed columns When storing JSON documents in SQL Server, We need to be able to efficiently filter and sort query results on properties of the JSON documents. Given the above table If we want to find the row with the name = 'Adam', we would execute the following query. However this will require SQL server to perform a full table which on a large table is not efficent. To speed this up we would like to add an index, however we cannot directly reference properties in the JSON document. The solution is to add a computed column on the JSON path Now when we execute the same query, instead of a full table scan SQL server uses an index to seek into the non-clustered index and find the rows that satisfy the specified conditions. Note: For SQL server to use the index, you must create the computed column with the same expression that you plan to use in your queries - in this example |
||||||||||||||||||
| Format one table row as a single JSON object using FOR JSON WITHOUT_ARRAY_WRAPPER option in FOR JSON clause will remove array brackets from the JSON output. This is useful if you are returning single row in the query. Note: this option will produce invalid JSON output if more than one row is returned. Input table data (People table)
Query Result |
||||||||||||||||||
| Parse JSON text using OPENJSON function OPENJSON function parses JSON text and returns multiple outputs. Values that should be returned are specified using the paths defined in the WITH clause. If a path is not specified for some column, the column name is used as a path. This function casts returned values to the SQL types defined in the WITH clause. AS JSON option must be specified in the column definition if some object/array should be returned. Result
Syntax
Parameters
RemarksThe OPENJSON function is only available under compatibility level 130. If your database compatibility level is lower than 130, SQL Server will not be able to find and execute OPENJSON function. Currently all Azure SQL databases are set to 120 by default. You can change the compatibility level of a database using the following command: |
Read Article
https://develop.horje.com/learn/1434/reference