{"id":621,"date":"2019-08-29T19:32:16","date_gmt":"2019-08-29T14:02:16","guid":{"rendered":"https:\/\/techieshouts.com\/?p=621"},"modified":"2022-08-09T19:06:25","modified_gmt":"2022-08-09T13:36:25","slug":"import-json-files-into-sql-server","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/import-json-files-into-sql-server\/","title":{"rendered":"Import JSON files into SQL Server"},"content":{"rendered":"\n<p>JavaScript Object Notation (JSON) is a lightweight data-interchange format. The JSON format is widely used to exchange data in most web and mobile applications. Also, the REST web services return results in JSON format. In this article, let&#8217;s see how to import JSON files into SQL Server.<\/p>\n\n\n\n<p>In SQL Server, you can import JSON documents, parse JSON text, transform an array of objects into a table format, and vice versa using built-in functions and operators.<\/p>\n\n\n\n<p>Like XML, there is no JSON data type to store JSON\ndata in a column. You can use either VARCHAR or NVARCHAR data type.<\/p>\n\n\n\n<h2><strong>Import JSON file as string<\/strong><\/h2>\n\n\n\n<p>You can use the <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/functions\/openrowset-transact-sql?view=sql-server-2017\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"OPENROWSET (opens in a new tab)\">OPENROWSET<\/a> function to read the data from a file. This function reads the entire file content as a string and returns the value as a table with a single column. The value can be stored in a table or a variable for further processing.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DECLARE @JSONText VARCHAR(4000)\nSELECT @JSONText = BulkColumn \nFROM OPENROWSET (BULK 'D:\\JSONFiles\\Employees.JSON', SINGLE_CLOB) as TableA\nSELECT @JSONText<\/pre>\n\n\n\n<p>Use the data\ntype &#8216;SINGLE_CLOB&#8217; to import the string data. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" width=\"1024\" height=\"200\" src=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/OpenRowset-1024x200.png\" alt=\"Import JSON using OPENROWSET\" class=\"wp-image-622\" srcset=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/OpenRowset-1024x200.png 1024w, https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/OpenRowset-300x59.png 300w, https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/OpenRowset-768x150.png 768w, https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/OpenRowset-660x129.png 660w, https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/OpenRowset.png 1059w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>To validate\nwhether the string is a valid JSON, use ISJSON function. It returns 1 when the\ninput is a valid JSON string.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">IF (ISJSON(@JSONText) = 1)\nBEGIN \nEND<\/pre>\n\n\n\n<h2><strong>Import JSON data into a table<\/strong><\/h2>\n\n\n\n<p> You can use <a href=\"https:\/\/techieshouts.com\/openjson-function-in-sql-server\/\">OPENJSON<\/a> table-valued function to parse the JSON data and transform it into a tabular format. It returns the objects and properties of the JSON text as rows and columns. <\/p>\n\n\n\n<h3><strong>Method 1: With default schema<\/strong><\/h3>\n\n\n\n<p>By default, the OPENJSON function returns three columns &#8211; key name, value, and the type of each key-value pair.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DECLARE @JSONText VARCHAR(4000) = N'\n[{\"EmpID\":101,\"FirstName\":\"Sean\",\"MiddleInitial\":\"E\",\"LastName\":\"Paul\"},  \n{\"EmpID\":201,\"FirstName\":\"John\",\"MiddleInitial\":\"R\",\"LastName\":\"Peter\"},  \n{\"EmpID\":301,\"FirstName\":\"Samantha\", \"MiddleInitial\":null, \"LastName\":\"Sam\"}]'\nSELECT * FROM OPENJSON(@JSONText)<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" width=\"681\" height=\"332\" src=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/OpenJSON1.png\" alt=\"Import JSON using OPENJSON default schema\" class=\"wp-image-623\" srcset=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/OpenJSON1.png 681w, https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/OpenJSON1-300x146.png 300w, https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/OpenJSON1-660x322.png 660w\" sizes=\"(max-width: 681px) 100vw, 681px\" \/><figcaption>OPENJSON with default schema<\/figcaption><\/figure><\/div>\n\n\n\n<p>Type column in the above image returns a number that denotes the data type of the value column. It ranges from 0 through 5 for various data types.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" width=\"217\" height=\"186\" src=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/JSON-Types.png\" alt=\"JSON Types\" class=\"wp-image-625\"\/><\/figure><\/div>\n\n\n\n<h3><strong>Method 2: With explicit schema definition<\/strong><\/h3>\n\n\n\n<p>As an alternative to the default method, you can define the schema of the result by using with clause. This method allows you to specify the column name and its data type. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DECLARE @JSONText VARCHAR(4000) = N'\n[{\"EmpID\":101,\"FirstName\":\"Sean\",\"MiddleInitial\":\"E\",\"LastName\":\"Paul\"},  \n{\"EmpID\":201,\"FirstName\":\"John\",\"MiddleInitial\":\"R\",\"LastName\":\"Peter\"},  \n{\"EmpID\":301,\"FirstName\":\"Samantha\", \"MiddleInitial\":null, \"LastName\":\"Sam\"}]'\nSELECT * FROM OPENJSON(@JSONText)\nWITH(EmpID INT, FirstName VARCHAR(25), MiddleInitial CHAR(1), LastName VARCHAR(25))<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"749\" height=\"362\" src=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/OpenJSON2.png\" alt=\"Import JSON using OPENJSON with explicit schema definition\" class=\"wp-image-624\" srcset=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/OpenJSON2.png 749w, https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/OpenJSON2-300x145.png 300w, https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/OpenJSON2-660x319.png 660w\" sizes=\"(max-width: 749px) 100vw, 749px\" \/><figcaption>OPENJSON with an explicit schema definition<\/figcaption><\/figure>\n\n\n\n<p>Below is the consolidated script to import, parse and validate JSON files.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DECLARE @JSONText VARCHAR(4000)\n--OPENROWSET method to read JSON file\nSELECT @JSONText = BulkColumn FROM OPENROWSET (BULK 'D:\\JSONFiles\\Employees.JSON', SINGLE_CLOB) AS TableA\nSELECT @JSONText\n--Validate JSON\nIF (ISJSON(@JSONText) = 1)\nBEGIN \n--OPENJSON Default Schema\nSELECT * FROM OPENJSON(@JSONText)\n--OPENJSON Explicit Schema\nSELECT * FROM OPENJSON(@JSONText)\nWITH(EmpID INT, FirstName VARCHAR(25), MiddleInitial CHAR(1), LastName VARCHAR(25))\nEND<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript Object Notation (JSON) is a lightweight data-interchange format. The JSON format is widely used to exchange data in most web and mobile applications. Also, the REST web services return results in JSON format. In this article, let&#8217;s see how to import JSON files into SQL Server. In SQL Server, you can import JSON documents,\u2026 <span class=\"read-more\"><a href=\"https:\/\/techieshouts.com\/home\/import-json-files-into-sql-server\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10,15],"tags":[59,222,60,58,39,40,41],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/621"}],"collection":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/comments?post=621"}],"version-history":[{"count":6,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/621\/revisions"}],"predecessor-version":[{"id":1076,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/621\/revisions\/1076"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}