1

I have a JSON structured like this:

{
"Assessment": {
    "AssessmentContext": {
        "ReportID": "1021060",
        "ReportName": "John Smith",
    },
}

I am using SQL server 2016 and this is my query:

SELECT Data.ReportID,Data.ReportName
FROM TableName DT
CROSS APPLY 
OPENJSON(DT.TX_TEXT) WITH (
ReportID INT '$.ReportID',
ReportName VARCHAR(100) '$.ReportName'
    ) AS Data

I am getting NULL values for both the columns when I run this query. What am I doing wrong?

Update:

I am editing the question as I am not able to format the text as code in comment. How do I tweak my sql to show Report Id, Report Name, Item Name, Value.

{
  "Assessment": {
"AssessmentContext": {
    "ReportID": "1021060",
    "ReportName": "John Smith",
},
},       

"Assessment": {
        "AssessmentID": "0",
        "AssessmentItems": [{
                "ItemName": "A",
                "Value": "N"                    
            }, {
                "ItemName": "B",
                "Value": "A"                    
            }]
}

1 Answer 1

1

You could pass path:

SELECT Data.ReportID,Data.ReportName
FROM TableName DT
CROSS APPLY 
OPENJSON(DT.TX_TEXT, '$.Assessment.AssessmentContext') -- here
WITH ( ReportID INT '$.ReportID',
       ReportName VARCHAR(100) '$.ReportName') AS Data

db<>fiddle demo

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much, it worked. Never knew it was so simple duh.

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.