Hi friends, we will continue with FOR JSON clause in this blog and today we will learn formatting of query using FOR JSON clause mode with ROOT option in SQL Server 2016.
As per BOL we need to specify ROOT option in query, when we want to add a single, top level element to the JSON output of FOR JSON clause. JSON output will not have a root element until we specify the root option.
Syntax for ROOT option is:
ROOT [(‘RootName’)]
If we don’t provide the name of root element then it will take default value as root.
Let’s execute the below query to see the JSON ROOT option output:
SELECT BusinessEntityID, SalesYTD, TerritoryId, SalesQuota FROM Sales.SalesPerson WHERE BusinessEntityId = 285 FOR JSON AUTO, ROOT ('Sales')
Here in output we can see that Sales is the root element.
{
“Sales”:
[{“BusinessEntityID”:285,
“SalesYTD”:172524.4512
}]
}
That’s all for the day folks.
Regards,
Kapil Singh Kumawat
Like us on FaceBook | Join the fastest growing SQL Server group on FaceBook