简体   繁体   中英

SQL - Parse JSON Array without column headers

I have the following json payload:

"rows": [["202003290528", "/home", "/home", "(not set)", "/home", "desktop", "Charlotte NC", "1", "1", "1", "0.0", "1", "1", "0.0", "1"], ["202003291516", "/home", "/home", "(not set)", "/home", "mobile", "Chicago IL", "1", "1", "1", "0.0", "1", "0", "0.0", "1"], ["202003291930", "/home", "/home", "(not set)", "/home", "mobile", "Boston MA-Manchester NH", "1", "1", "1", "0.0", "1", "0", "0.0", "1"], ["202003291942", "/home", "/home", "(not set)", "/home", "mobile", "Des Moines-Ames IA", "1", "1", "1", "0.0", "1", "0", "0.0", "1"]]

How can I parse this to fill a SQL table using SQL Server 2016:

dateHourMinute  pagePath    landingPagePath secondPagePath  exitPagePath    deviceCategory  metro   sessions    pageviews   uniquePageviews timeOnPage  users   newUsers    sessionDuration bounces

You need to use OPENJSON() with explicit schema. Just use the appropriate column names in the WITH clause:

Statement:

DECLARE @json nvarchar(max) = N'{
   "rows":[
      [
         "202003290528",
         "/home",
         "/home",
         "(not set)",
         "/home",
         "desktop",
         "Charlotte NC",
         "1",
         "1",
         "1",
         "0.0",
         "1",
         "1",
         "0.0",
         "1"
      ],
      [
         "202003291516",
         "/home",
         "/home",
         "(not set)",
         "/home",
         "mobile",
         "Chicago IL",
         "1",
         "1",
         "1",
         "0.0",
         "1",
         "0",
         "0.0",
         "1"
      ],
      [
         "202003291930",
         "/home",
         "/home",
         "(not set)",
         "/home",
         "mobile",
         "Boston MA-Manchester NH",
         "1",
         "1",
         "1",
         "0.0",
         "1",
         "0",
         "0.0",
         "1"
      ],
      [
         "202003291942",
         "/home",
         "/home",
         "(not set)",
         "/home",
         "mobile",
         "Des Moines-Ames IA",
         "1",
         "1",
         "1",
         "0.0",
         "1",
         "0",
         "0.0",
         "1"
      ]
   ]
}'

Statement:

SELECT *
FROM OPENJSON(@json, '$.rows') WITH (
   dateHourMinute varchar(100) '$[0]',
   pagePath varchar(100) '$[1]',
   landingPagePath varchar(100) '$[2]',
   secondPagePath varchar(100) '$[3]',
   exitPagePath varchar(100) '$[4]',
   deviceCategory varchar(100) '$[5]',
   metro varchar(100) '$[6]',
   sessions varchar(100) '$[7]',
   pageviews varchar(100) '$[8]',
   uniquePageviews varchar(100) '$[9]',
   timeOnPage varchar(100) '$[10]',
   users varchar(100) '$[11]',
   newUsers varchar(100) '$[12]',
   sessionDuration varchar(100) '$[13]',
   bounces varchar(100) '$[14]'
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM