0

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

1 Answer 1

1

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]'
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.