{"id":30356,"date":"2015-11-29T11:39:00","date_gmt":"2015-11-29T06:09:00","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=30356"},"modified":"2016-02-09T10:51:56","modified_gmt":"2016-02-09T05:21:56","slug":"how-to-get-output-sql-server-as-a-json-string","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-get-output-sql-server-as-a-json-string\/","title":{"rendered":"How to get output from Sql server as a jSON String ?"},"content":{"rendered":"<p>We can convert tabular format to xml using sql query in sql server database ,but if we want to convert in json format \u00a0from database ,then we can face problem because database does not support \u00a0native JSON integration. There is a way through which we can generate output in json format by following these steps:-<\/p>\n<p>1.Convert tabular format to xml .<\/p>\n<p>2.Generate json format from that xml by using custom select query as \u00a0in given below example step 3.<\/p>\n<p>This is step by step process to convert sql output to json format as:-<\/p>\n<p><strong>1)<\/strong> <strong>Let\u2019s create Product list table in sql server and insert some record in this table<\/strong>.<\/p>\n<p>createtableProductList<br \/>\n(<br \/>\nId int identity(1,1) primary key,<br \/>\nProductName varchar(500),<br \/>\nPrice numeric(18,2),<br \/>\nQuantity int,<br \/>\nInserton datetime<br \/>\n)<\/p>\n<p>insert into ProductList(ProductName,Price,quantity,Inserton)<br \/>\nvalues(&#8216;Digital camera&#8217;,20000,5,getdate())<\/p>\n<p>insert into ProductList(ProductName,Price,quantity,Inserton)<br \/>\nvalues(&#8216;Laptop&#8217;,10000,5,getdate())<\/p>\n<p>insert into ProductList(ProductName,Price,quantity,Inserton)<br \/>\nvalues(&#8216;Printer&#8217;,12000.00,5,getdate())<\/p>\n<p>insert into ProductList(ProductName,Price,quantity,Inserton)<br \/>\nvalues(&#8216;Mobile Phone&#8217;,12000.00,5,getdate())<\/p>\n<p>insert into ProductList(ProductName,Price,quantity,Inserton)<br \/>\nvalues(&#8216;Digital camera&#8217;,20000.00, 5,getdate())<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-30354\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/11\/ShowProduct.png\" alt=\"ShowProduct\" width=\"392\" height=\"118\" \/><\/p>\n<p><strong>2)<\/strong> <strong>Convert sql table into \u00a0XML Format<\/strong><\/p>\n<p>Select * from ProductList for XML Path(&#8216;xml path&#8217;)<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-30355\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/11\/XML.png\" alt=\"XML\" width=\"392\" height=\"328\" \/><\/p>\n<p><strong>3)<\/strong> <strong>Convert XML To JSon format string by using custom select query.<\/strong><\/p>\n<p>select'[&#8216;+<br \/>\nstuff(<br \/>\n(<br \/>\nselect top 2 &#8216;,{&#8220;ProductName&#8221;: &#8220;&#8216;+ProductName+'&#8221;,&#8221;Price&#8221;: &#8220;&#8216;+convert(varchar,Price)+'&#8221;,&#8221;Quantity&#8221;: &#8220;&#8216;+convert(varchar,quantity)+'&#8221;,&#8221;Inserton&#8221;: &#8220;&#8216;+convert(varchar,Inserton,105)+'&#8221;}&#8217;<br \/>\nfrom ProductList for XML Path(&#8221;)<br \/>\n),1,1,&#8221;<br \/>\n)<br \/>\n+&#8217;]'[ProductDetail]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-30353\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/11\/JSON.png\" alt=\"JSON\" width=\"392\" height=\"233\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We can convert tabular format to xml using sql query in sql server database ,but if we want to convert in json format \u00a0from database ,then we can face problem because database does not support \u00a0native JSON integration. There is a way through which we can generate output in json format by following these steps:- [&hellip;]<\/p>\n","protected":false},"author":855,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":34},"categories":[1],"tags":[2867],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/30356"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/855"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=30356"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/30356\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=30356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=30356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=30356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}