0

I have the following JSON

DECLARE @json NVARCHAR(max) = '
{
  "source": "one",
  "dataSetId":"a3d5-14fda14",
  "data": [
    {
      "FieldId": 10
    }
  ]
}
'

and need to parse the values to the table.

I have tried:

-- meta
SELECT *  
FROM OPENJSON(@json)  
  WITH ( source NVARCHAR(20)
        ,dataSetId NVARCHAR(50)
        ,FieldId INT '$.data.FieldId'
)

-- array
SELECT * 
FROM OPENJSON(@json, '$.data')
  WITH ( source NVARCHAR(20)
        ,dataSetId NVARCHAR(50)
        ,FieldId int '$.FieldId'
)

It works to get source and dataSetId columns or to get FieldId column, but I am still unable to merge both solution, to parse both non-array and array data.

The SELECT should return all the data in one query.

3
  • If you tried then What sql error you getting. Commented Oct 5, 2017 at 8:47
  • Yes, I did. You can see the queries in the question. There's no error. .. it works, but does not return all the data from JSON in one query. Commented Oct 5, 2017 at 8:53
  • Should you try with by defining a temp table and store your result into temp table i.e #myResultTable and pass it to OPENJSON Commented Oct 5, 2017 at 10:26

1 Answer 1

1

There are many solutions for this question. one of them is....

DECLARE @json NVARCHAR(max) = '
 {
   "source": "one",
   "dataSetId":"a3d5-14fda14",
   "data": [
    {
      "FieldId": 10
    }
  ]
}'

SELECT  A.* , B.*  
FROM OPENJSON(@json) 
  WITH ( source NVARCHAR(20)
        ,dataSetId NVARCHAR(50)) A ,  
 OPENJSON(@json, '$.data') 
  WITH (  FieldId int '$.FieldId') B
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.