{"id":3549,"date":"2024-03-13T15:20:49","date_gmt":"2024-03-13T08:20:49","guid":{"rendered":"https:\/\/www.sqlservertutorial.net\/?page_id=3549"},"modified":"2024-03-14T13:56:43","modified_gmt":"2024-03-14T06:56:43","slug":"sql-server-for-json","status":"publish","type":"page","link":"https:\/\/www.sqlservertutorial.net\/sql-server-json-functions\/sql-server-for-json\/","title":{"rendered":"SQL Server FOR JSON"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the SQL Server <code>FOR JSON<\/code> clause to format query results as JSON text.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-sql-server-for-json-clause'>Introduction to the SQL Server FOR JSON clause <a href=\"#introduction-to-the-sql-server-for-json-clause\" class=\"anchor\" id=\"introduction-to-the-sql-server-for-json-clause\" title=\"Anchor for Introduction to the SQL Server FOR JSON clause\">#<\/a><\/h2>\n\n\n\n<p>The <code>FOR JSON<\/code> clause allows you to format a query result as <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-json\/\">JSON<\/a> text. <\/p>\n\n\n\n<p>The <code>FOR JSON<\/code> clause can be useful when you want to retrieve data from SQL Server and serialize it as JSON for consumption by applications or web services.<\/p>\n\n\n\n<p>Here&#8217;s the basic syntax of the <code>FOR JSON<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> select_list\n<span class=\"hljs-keyword\">FROM<\/span> table_name\n<span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-keyword\">JSON<\/span> <span class=\"hljs-keyword\">AUTO<\/span> | <span class=\"hljs-keyword\">PATH<\/span>, extra_option;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, specify the <code>FOR JSON<\/code> clause at the end of a query.<\/li>\n\n\n\n<li>Second, provide the JSON output format in the JSON format. The <code>FOR JSON AUTO<\/code> formats JSON automatically based on the query set whereas the <code>FOR JSON PATH<\/code> allows you to have full control over the format of the JSON output.<\/li>\n<\/ul>\n\n\n\n<p>The following extra options allow you to control the output of the JSON document:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ROOT<\/code>: Add a single, top-level element to the JSON output. If you skip this, the JSON output won&#8217;t have a root element.<\/li>\n\n\n\n<li><code>INCLUDE_NULL_VALUES<\/code>: The function doesn&#8217;t include NULL in the output JSON. If you want to convert NULL to null values and include them in the output JSON, you can use the <code>INCLUDE_NULL_VALUES<\/code> option.<\/li>\n\n\n\n<li><code>WITHOUT_ARRAY_WRAPPER<\/code>: Use this option to format a single-row result as an object. Otherwise, the function will place the JSON object into a JSON array.<\/li>\n<\/ul>\n\n\n\n<p>The <code>FOR JSON<\/code> is a counterpart of the <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-json-functions\/sql-server-openjson\/\">OPENJSON()<\/a> function, which converts a JSON document to rows and columns.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='sql-server-for-json-clause-examples'>SQL Server FOR JSON clause examples <a href=\"#sql-server-for-json-clause-examples\" class=\"anchor\" id=\"sql-server-for-json-clause-examples\" title=\"Anchor for SQL Server FOR JSON clause examples\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s take some examples of using the <code>FOR JSON<\/code> clause.<\/p>\n\n\n\n<p>We&#8217;ll use the <code>products<\/code> and <code>categories<\/code> tables from the <a href=\"https:\/\/www.sqlservertutorial.net\/getting-started\/sql-server-sample-database\/\">sample database<\/a>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"494\" height=\"169\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/products-categories.png\" alt=\"\" class=\"wp-image-145\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/products-categories.png 494w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/products-categories-300x103.png 300w\" sizes=\"auto, (max-width: 494px) 100vw, 494px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='1-basic-sql-server-for-json-clause-example'>1) Basic SQL Server FOR JSON clause example <a href=\"#1-basic-sql-server-for-json-clause-example\" class=\"anchor\" id=\"1-basic-sql-server-for-json-clause-example\" title=\"Anchor for 1) Basic SQL Server FOR JSON clause example\">#<\/a><\/h3>\n\n\n\n<p>The following example retrieves the top 5 most expensive products and formats the result set as JSON using the <code>FOR JSON<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n  TOP <span class=\"hljs-number\">5<\/span> product_id, \n  product_name, \n  list_price \n<span class=\"hljs-keyword\">FROM<\/span> \n  production.products \n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n  list_price <span class=\"hljs-keyword\">DESC<\/span> \n<span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-keyword\">JSON<\/span> <span class=\"hljs-keyword\">AUTO<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">&#91;\n  {\n    \"product_id\": 155,\n    \"product_name\": \"Trek Domane SLR 9 Disc - 2018\",\n    \"list_price\": 11999.99\n  },\n  {\n    \"product_id\": 149,\n    \"product_name\": \"Trek Domane SLR 8 Disc - 2018\",\n    \"list_price\": 7499.99\n  },\n  {\n    \"product_id\": 51,\n    \"product_name\": \"Trek Silque SLR 8 Women's - 2017\",\n    \"list_price\": 6499.99\n  },\n  {\n    \"product_id\": 156,\n    \"product_name\": \"Trek Domane SL Frameset - 2018\",\n    \"list_price\": 6499.99\n  },\n  {\n    \"product_id\": 157,\n    \"product_name\": \"Trek Domane SL Frameset Women's - 2018\",\n    \"list_price\": 6499.99\n  }\n]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='2-using-the-for-json-clause-with-a-specified-root-document'>2) Using the FOR JSON clause with a specified root document <a href=\"#2-using-the-for-json-clause-with-a-specified-root-document\" class=\"anchor\" id=\"2-using-the-for-json-clause-with-a-specified-root-document\" title=\"Anchor for 2) Using the FOR JSON clause with a specified root document\">#<\/a><\/h3>\n\n\n\n<p>The following example retrieves the top 5 most expensive products and formats the result set with the root document as products:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n  TOP <span class=\"hljs-number\">5<\/span> product_id, \n  product_name, \n  list_price \n<span class=\"hljs-keyword\">FROM<\/span> \n  production.products \n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n  list_price <span class=\"hljs-keyword\">DESC<\/span> \n<span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-keyword\">JSON<\/span> <span class=\"hljs-keyword\">AUTO<\/span>, ROOT(<span class=\"hljs-string\">'products'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">{\n  \"products\": &#91;\n    {\n      \"product_id\": 155,\n      \"product_name\": \"Trek Domane SLR 9 Disc - 2018\",\n      \"list_price\": 11999.99\n    },\n    {\n      \"product_id\": 149,\n      \"product_name\": \"Trek Domane SLR 8 Disc - 2018\",\n      \"list_price\": 7499.99\n    },\n    {\n      \"product_id\": 51,\n      \"product_name\": \"Trek Silque SLR 8 Women's - 2017\",\n      \"list_price\": 6499.99\n    },\n    {\n      \"product_id\": 156,\n      \"product_name\": \"Trek Domane SL Frameset - 2018\",\n      \"list_price\": 6499.99\n    },\n    {\n      \"product_id\": 157,\n      \"product_name\": \"Trek Domane SL Frameset Women's - 2018\",\n      \"list_price\": 6499.99\n    }\n  ]\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='3-using-the-for-json-clause-with-aggregate-functions'>3) Using the FOR JSON clause with aggregate functions <a href=\"#3-using-the-for-json-clause-with-aggregate-functions\" class=\"anchor\" id=\"3-using-the-for-json-clause-with-aggregate-functions\" title=\"Anchor for 3) Using the FOR JSON clause with aggregate functions\">#<\/a><\/h3>\n\n\n\n<p>The following example retrieves the category and product count in each category, returning the result set as JSON text:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n  c.category_name, \n  <span class=\"hljs-keyword\">COUNT<\/span>(*) product_count \n<span class=\"hljs-keyword\">FROM<\/span> \n  production.categories c \n  <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> production.products p <span class=\"hljs-keyword\">on<\/span> c.category_id = p.category_id \n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> \n  c.category_name \n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n  product_count <span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-keyword\">JSON<\/span> <span class=\"hljs-keyword\">AUTO<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">&#91;\n  {\n    \"category_name\": \"Cyclocross Bicycles\",\n    \"product_count\": 10\n  },\n  {\n    \"category_name\": \"Electric Bikes\",\n    \"product_count\": 24\n  },\n  {\n    \"category_name\": \"Comfort Bicycles\",\n    \"product_count\": 30\n  },\n  {\n    \"category_name\": \"Children Bicycles\",\n    \"product_count\": 59\n  },\n  {\n    \"category_name\": \"Mountain Bikes\",\n    \"product_count\": 60\n  },\n  {\n    \"category_name\": \"Road Bikes\",\n    \"product_count\": 60\n  },\n  {\n    \"category_name\": \"Cruisers Bicycles\",\n    \"product_count\": 78\n  }\n]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='4-using-the-for-json-clause-with-a-single-row-result'>4) Using the FOR JSON clause with a single-row result <a href=\"#4-using-the-for-json-clause-with-a-single-row-result\" class=\"anchor\" id=\"4-using-the-for-json-clause-with-a-single-row-result\" title=\"Anchor for 4) Using the FOR JSON clause with a single-row result\">#<\/a><\/h3>\n\n\n\n<p>The following example retrieves a product with id 1 from the <code>production.products<\/code> table and format the result set as JSON using the <code>FOR JSON AUTO<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">SELECT \n  product_id, \n  product_name, \n  list_price \nFROM \n  production.products \nWHERE \n   product_id = <span class=\"hljs-number\">1<\/span>\nFOR <span class=\"hljs-built_in\">JSON<\/span> AUTO;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">&#91;\n  {\n    <span class=\"hljs-attr\">\"product_id\"<\/span>: <span class=\"hljs-number\">1<\/span>,\n    <span class=\"hljs-attr\">\"product_name\"<\/span>: <span class=\"hljs-string\">\"Trek 820 - 2016\"<\/span>,\n    <span class=\"hljs-attr\">\"list_price\"<\/span>: <span class=\"hljs-number\">379.99<\/span>\n  }\n]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The output JSON is an array that consists of one JSON object.<\/p>\n\n\n\n<p>If you use the <code>WITHOUT_ARRAY_WRAPPER<\/code> option, the FOR JSON clause will return a single JSON object instead. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">SELECT \n  product_id, \n  product_name, \n  list_price \nFROM \n  production.products \nWHERE \n   product_id = <span class=\"hljs-number\">1<\/span>\nFOR <span class=\"hljs-built_in\">JSON<\/span> AUTO, WITHOUT_ARRAY_WRAPPER;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">{\n  <span class=\"hljs-attr\">\"product_id\"<\/span>: <span class=\"hljs-number\">1<\/span>,\n  <span class=\"hljs-attr\">\"product_name\"<\/span>: <span class=\"hljs-string\">\"Trek 820 - 2016\"<\/span>,\n  <span class=\"hljs-attr\">\"list_price\"<\/span>: <span class=\"hljs-number\">379.99<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='5-using-for-json-clause-to-create-nested-objects'>5) Using FOR JSON clause to create nested objects <a href=\"#5-using-for-json-clause-to-create-nested-objects\" class=\"anchor\" id=\"5-using-for-json-clause-to-create-nested-objects\" title=\"Anchor for 5) Using FOR JSON clause to create nested objects\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>FOR JSON<\/code> clause to create JSON that includes orders and order items of the customer id 1:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \r\n    o.order_id,\r\n    o.order_status,\r\n    o.order_date,\r\n    (\r\n        <span class=\"hljs-keyword\">SELECT<\/span> \r\n            oi.item_id,\r\n            oi.product_id,\r\n            oi.quantity,\r\n            oi.list_price,\r\n            oi.discount\r\n        <span class=\"hljs-keyword\">FROM<\/span> \r\n            sales.order_items oi\r\n        <span class=\"hljs-keyword\">WHERE<\/span> \r\n            oi.order_id = o.order_id\r\n        <span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-keyword\">JSON<\/span> <span class=\"hljs-keyword\">PATH<\/span>\r\n    ) <span class=\"hljs-keyword\">AS<\/span> items\r\n<span class=\"hljs-keyword\">FROM<\/span> \r\n    sales.orders o\r\n<span class=\"hljs-keyword\">WHERE<\/span> \r\n    o.customer_id = <span class=\"hljs-number\">1<\/span>\r\n<span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-keyword\">JSON<\/span> <span class=\"hljs-keyword\">PATH<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">&#91;\n  {\n    <span class=\"hljs-attr\">\"order_id\"<\/span>: <span class=\"hljs-number\">599<\/span>,\n    <span class=\"hljs-attr\">\"order_status\"<\/span>: <span class=\"hljs-number\">4<\/span>,\n    <span class=\"hljs-attr\">\"order_date\"<\/span>: <span class=\"hljs-string\">\"2016-12-09\"<\/span>,\n    <span class=\"hljs-attr\">\"items\"<\/span>: &#91;\n      {\n        <span class=\"hljs-attr\">\"item_id\"<\/span>: <span class=\"hljs-number\">1<\/span>,\n        <span class=\"hljs-attr\">\"product_id\"<\/span>: <span class=\"hljs-number\">9<\/span>,\n        <span class=\"hljs-attr\">\"quantity\"<\/span>: <span class=\"hljs-number\">2<\/span>,\n        <span class=\"hljs-attr\">\"list_price\"<\/span>: <span class=\"hljs-number\">2999.99<\/span>,\n        <span class=\"hljs-attr\">\"discount\"<\/span>: <span class=\"hljs-number\">0.05<\/span>\n      },\n      {\n        <span class=\"hljs-attr\">\"item_id\"<\/span>: <span class=\"hljs-number\">2<\/span>,\n        <span class=\"hljs-attr\">\"product_id\"<\/span>: <span class=\"hljs-number\">22<\/span>,\n        <span class=\"hljs-attr\">\"quantity\"<\/span>: <span class=\"hljs-number\">2<\/span>,\n        <span class=\"hljs-attr\">\"list_price\"<\/span>: <span class=\"hljs-number\">269.99<\/span>,\n        <span class=\"hljs-attr\">\"discount\"<\/span>: <span class=\"hljs-number\">0.2<\/span>\n      },\n      {\n        <span class=\"hljs-attr\">\"item_id\"<\/span>: <span class=\"hljs-number\">3<\/span>,\n        <span class=\"hljs-attr\">\"product_id\"<\/span>: <span class=\"hljs-number\">23<\/span>,\n        <span class=\"hljs-attr\">\"quantity\"<\/span>: <span class=\"hljs-number\">1<\/span>,\n        <span class=\"hljs-attr\">\"list_price\"<\/span>: <span class=\"hljs-number\">299.99<\/span>,\n        <span class=\"hljs-attr\">\"discount\"<\/span>: <span class=\"hljs-number\">0.07<\/span>\n      },\n      {\n        <span class=\"hljs-attr\">\"item_id\"<\/span>: <span class=\"hljs-number\">4<\/span>,\n        <span class=\"hljs-attr\">\"product_id\"<\/span>: <span class=\"hljs-number\">10<\/span>,\n        <span class=\"hljs-attr\">\"quantity\"<\/span>: <span class=\"hljs-number\">2<\/span>,\n        <span class=\"hljs-attr\">\"list_price\"<\/span>: <span class=\"hljs-number\">1549<\/span>,\n        <span class=\"hljs-attr\">\"discount\"<\/span>: <span class=\"hljs-number\">0.1<\/span>\n      }\n    ]\n  },\n  {\n    <span class=\"hljs-attr\">\"order_id\"<\/span>: <span class=\"hljs-number\">1555<\/span>,\n    <span class=\"hljs-attr\">\"order_status\"<\/span>: <span class=\"hljs-number\">1<\/span>,\n    <span class=\"hljs-attr\">\"order_date\"<\/span>: <span class=\"hljs-string\">\"2018-04-18\"<\/span>,\n    <span class=\"hljs-attr\">\"items\"<\/span>: &#91;\n      {\n        <span class=\"hljs-attr\">\"item_id\"<\/span>: <span class=\"hljs-number\">1<\/span>,\n        <span class=\"hljs-attr\">\"product_id\"<\/span>: <span class=\"hljs-number\">24<\/span>,\n        <span class=\"hljs-attr\">\"quantity\"<\/span>: <span class=\"hljs-number\">2<\/span>,\n        <span class=\"hljs-attr\">\"list_price\"<\/span>: <span class=\"hljs-number\">549.99<\/span>,\n        <span class=\"hljs-attr\">\"discount\"<\/span>: <span class=\"hljs-number\">0.1<\/span>\n      },\n      {\n        <span class=\"hljs-attr\">\"item_id\"<\/span>: <span class=\"hljs-number\">2<\/span>,\n        <span class=\"hljs-attr\">\"product_id\"<\/span>: <span class=\"hljs-number\">156<\/span>,\n        <span class=\"hljs-attr\">\"quantity\"<\/span>: <span class=\"hljs-number\">1<\/span>,\n        <span class=\"hljs-attr\">\"list_price\"<\/span>: <span class=\"hljs-number\">6499.99<\/span>,\n        <span class=\"hljs-attr\">\"discount\"<\/span>: <span class=\"hljs-number\">0.1<\/span>\n      },\n      {\n        <span class=\"hljs-attr\">\"item_id\"<\/span>: <span class=\"hljs-number\">3<\/span>,\n        <span class=\"hljs-attr\">\"product_id\"<\/span>: <span class=\"hljs-number\">126<\/span>,\n        <span class=\"hljs-attr\">\"quantity\"<\/span>: <span class=\"hljs-number\">1<\/span>,\n        <span class=\"hljs-attr\">\"list_price\"<\/span>: <span class=\"hljs-number\">469.99<\/span>,\n        <span class=\"hljs-attr\">\"discount\"<\/span>: <span class=\"hljs-number\">0.1<\/span>\n      },\n      {\n        <span class=\"hljs-attr\">\"item_id\"<\/span>: <span class=\"hljs-number\">4<\/span>,\n        <span class=\"hljs-attr\">\"product_id\"<\/span>: <span class=\"hljs-number\">128<\/span>,\n        <span class=\"hljs-attr\">\"quantity\"<\/span>: <span class=\"hljs-number\">2<\/span>,\n        <span class=\"hljs-attr\">\"list_price\"<\/span>: <span class=\"hljs-number\">1899<\/span>,\n        <span class=\"hljs-attr\">\"discount\"<\/span>: <span class=\"hljs-number\">0.05<\/span>\n      },\n      {\n        <span class=\"hljs-attr\">\"item_id\"<\/span>: <span class=\"hljs-number\">5<\/span>,\n        <span class=\"hljs-attr\">\"product_id\"<\/span>: <span class=\"hljs-number\">174<\/span>,\n        <span class=\"hljs-attr\">\"quantity\"<\/span>: <span class=\"hljs-number\">1<\/span>,\n        <span class=\"hljs-attr\">\"list_price\"<\/span>: <span class=\"hljs-number\">3199.99<\/span>,\n        <span class=\"hljs-attr\">\"discount\"<\/span>: <span class=\"hljs-number\">0.2<\/span>\n      }\n    ]\n  },\n  {\n    <span class=\"hljs-attr\">\"order_id\"<\/span>: <span class=\"hljs-number\">1613<\/span>,\n    <span class=\"hljs-attr\">\"order_status\"<\/span>: <span class=\"hljs-number\">3<\/span>,\n    <span class=\"hljs-attr\">\"order_date\"<\/span>: <span class=\"hljs-string\">\"2018-11-18\"<\/span>,\n    <span class=\"hljs-attr\">\"items\"<\/span>: &#91;\n      {\n        <span class=\"hljs-attr\">\"item_id\"<\/span>: <span class=\"hljs-number\">1<\/span>,\n        <span class=\"hljs-attr\">\"product_id\"<\/span>: <span class=\"hljs-number\">153<\/span>,\n        <span class=\"hljs-attr\">\"quantity\"<\/span>: <span class=\"hljs-number\">1<\/span>,\n        <span class=\"hljs-attr\">\"list_price\"<\/span>: <span class=\"hljs-number\">4999.99<\/span>,\n        <span class=\"hljs-attr\">\"discount\"<\/span>: <span class=\"hljs-number\">0.07<\/span>\n      },\n      {\n        <span class=\"hljs-attr\">\"item_id\"<\/span>: <span class=\"hljs-number\">2<\/span>,\n        <span class=\"hljs-attr\">\"product_id\"<\/span>: <span class=\"hljs-number\">283<\/span>,\n        <span class=\"hljs-attr\">\"quantity\"<\/span>: <span class=\"hljs-number\">2<\/span>,\n        <span class=\"hljs-attr\">\"list_price\"<\/span>: <span class=\"hljs-number\">319.99<\/span>,\n        <span class=\"hljs-attr\">\"discount\"<\/span>: <span class=\"hljs-number\">0.05<\/span>\n      }\n    ]\n  }\n]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>FOR JSON<\/code> clause to format a query&#8217;s result into a JSON document.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"3549\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-json-functions\/sql-server-for-json\/\"\n\t\t\t\tdata-post-title=\"SQL Server FOR JSON\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"3549\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-json-functions\/sql-server-for-json\/\"\n\t\t\t\tdata-post-title=\"SQL Server FOR JSON\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to use the SQL Server FOR JSON clause to format query results as JSON text.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3480,"menu_order":9,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3549","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL Server FOR JSON Clause<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn how to use the SQL Server FOR JSON clause to format query results as JSON text.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlservertutorial.net\/sql-server-json-functions\/sql-server-for-json\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Server FOR JSON Clause\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn how to use the SQL Server FOR JSON clause to format query results as JSON text.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlservertutorial.net\/sql-server-json-functions\/sql-server-for-json\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Server Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-14T06:56:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/products-categories.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-json-functions\\\/sql-server-for-json\\\/\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-json-functions\\\/sql-server-for-json\\\/\",\"name\":\"SQL Server FOR JSON Clause\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-json-functions\\\/sql-server-for-json\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-json-functions\\\/sql-server-for-json\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/products-categories.png\",\"datePublished\":\"2024-03-13T08:20:49+00:00\",\"dateModified\":\"2024-03-14T06:56:43+00:00\",\"description\":\"In this tutorial, you will learn how to use the SQL Server FOR JSON clause to format query results as JSON text.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-json-functions\\\/sql-server-for-json\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-json-functions\\\/sql-server-for-json\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-json-functions\\\/sql-server-for-json\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/products-categories.png\",\"contentUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/products-categories.png\",\"width\":494,\"height\":169},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-json-functions\\\/sql-server-for-json\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server JSON Functions\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-json-functions\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"SQL Server FOR JSON\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\",\"name\":\"SQL Server Tutorial\",\"description\":\"The Practical SQL Server Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Server FOR JSON Clause","description":"In this tutorial, you will learn how to use the SQL Server FOR JSON clause to format query results as JSON text.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlservertutorial.net\/sql-server-json-functions\/sql-server-for-json\/","og_locale":"en_US","og_type":"article","og_title":"SQL Server FOR JSON Clause","og_description":"In this tutorial, you will learn how to use the SQL Server FOR JSON clause to format query results as JSON text.","og_url":"https:\/\/www.sqlservertutorial.net\/sql-server-json-functions\/sql-server-for-json\/","og_site_name":"SQL Server Tutorial","article_modified_time":"2024-03-14T06:56:43+00:00","og_image":[{"url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/products-categories.png","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-json-functions\/sql-server-for-json\/","url":"https:\/\/www.sqlservertutorial.net\/sql-server-json-functions\/sql-server-for-json\/","name":"SQL Server FOR JSON Clause","isPartOf":{"@id":"https:\/\/www.sqlservertutorial.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-json-functions\/sql-server-for-json\/#primaryimage"},"image":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-json-functions\/sql-server-for-json\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/products-categories.png","datePublished":"2024-03-13T08:20:49+00:00","dateModified":"2024-03-14T06:56:43+00:00","description":"In this tutorial, you will learn how to use the SQL Server FOR JSON clause to format query results as JSON text.","breadcrumb":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-json-functions\/sql-server-for-json\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlservertutorial.net\/sql-server-json-functions\/sql-server-for-json\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-json-functions\/sql-server-for-json\/#primaryimage","url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/products-categories.png","contentUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/products-categories.png","width":494,"height":169},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-json-functions\/sql-server-for-json\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlservertutorial.net\/"},{"@type":"ListItem","position":2,"name":"SQL Server JSON Functions","item":"https:\/\/www.sqlservertutorial.net\/sql-server-json-functions\/"},{"@type":"ListItem","position":3,"name":"SQL Server FOR JSON"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlservertutorial.net\/#website","url":"https:\/\/www.sqlservertutorial.net\/","name":"SQL Server Tutorial","description":"The Practical SQL Server Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlservertutorial.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/3549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/comments?post=3549"}],"version-history":[{"count":5,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/3549\/revisions"}],"predecessor-version":[{"id":3567,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/3549\/revisions\/3567"}],"up":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/3480"}],"wp:attachment":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/media?parent=3549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}