0

We've JSON entry in the MS SQL database. I would like to export JSON entry in the "Data" column which match with the list of EMPNO. Could someone please help?

ColumnName: Data

Data inside the column:

output:{
   "Request":{
      "Person":{
         "DisplayName":"Test User",
         "EMPNO":"000001",
         "Entity":"01",
         "Country":"AA"
      },
      "DomainOverride":null,
      "ReasonForGen":"Only",
      "Email":"[email protected]",
      "CurrentSIP":"[email protected]"
   },
   "EmailAddress":"123@testcom",
   "SIPAddress":"[email protected]",
   "Status":"NoChange"
}

Query in layman language:

select DisplayName,EMPNO,Entity,Country,DomainOverride,ReasonForGen,Email
from Table1
where data.output.Request.EMPNO in ([EMPNO list])

2 Answers 2

1

You can use JSON_VALUE. Something like this:

select JSON_VALUE(data,'$.Output.Request.Person.DisplayName'), ...
from Table1
where JSON_VALUE(data,'$.Output.Request.Person.EMPNO') in ([EMPNO list])
Sign up to request clarification or add additional context in comments.

Comments

1

You may try to use OPENJSON() to parse the JSON text and get objects and values from the JSON input as a table. You need to use OPENJSON() with explicit schema in the WITH clause to define the columns:

Table:

CREATE TABLE Data (
   JsonData nvarchar(max)
)
INSERT INTO Data 
   (JsonData)
VALUES
   (N'{
   "output":{
      "Request":{
         "Person":{
            "DisplayName":"Test User",
            "EMPNO":"000001",
            "Entity":"01",
            "Country":"AA"
         },
         "DomainOverride":null,
         "ReasonForGen":"Only",
         "Email":"[email protected]",
         "CurrentSIP":"[email protected]"
      },
      "EmailAddress":"123@testcom",
      "SIPAddress":"[email protected]",
      "Status":"NoChange"
   }
}')

Statement:

SELECT
   j.DisplayName,
   j.EMPNO,
   j.Entity,
   j.Country,
   j.DomainOverride,
   j.ReasonForGen,
   j.Email
FROM Data d
CROSS APPLY OPENJSON(d.JsonData) WITH (
   EMPNO nvarchar(10) '$.output.Request.Person.EMPNO',
   DisplayName nvarchar(50) '$.output.Request.Person.DisplayName',
   EMPNO nvarchar(50) '$.output.Request.Person.EMPNO',
   Entity nvarchar(50) '$.output.Request.Person.Entity',
   Country nvarchar(50) '$.output.Request.Person.Country',
   DomainOverride nvarchar(50) '$.output.Request.DomainOverride',
   ReasonForGen nvarchar(50) '$.output.Request.ReasonForGen',
   Email nvarchar(50) '$.output.Request.Email'
) j
-- Use additional WHERE clause
--WHERE j.EMPNO IN ('00001', '000002')

Result:

DisplayName EMPNO   Entity  Country DomainOverride  ReasonForGen    Email
Test User   000001  01      AA                      Only            [email protected]

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.