Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get Array Index from JSON data SQL Server

{
    "Name": ["dokumen_1","dokumen_2","dokumen_3","dokumen_4"],
    "Date": [0,0,0,0],
    "Progress": [0,0,0,0]
}

I want to fetch Date and Progress value according to Name position

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I changed the date and progress values for a better illustration

NOTE: in 2016 the JSON_VALUE has to be a literal

Example

Declare @JSON varchar(max) = '
{
"Name":["dokumen_1","dokumen_2","dokumen_3","dokumen_4"],
"Date":[1,2,3,4],
"Progress":[11,12,13,12]
}'


Select Name     = value
      ,Date     = JSON_VALUE(@JSON,'$.Date['+[key]+']')
      ,Progress = JSON_VALUE(@JSON,'$.Progress['+[key]+']')
 From  openjson(@JSON,N'$.Name')

Results

Name        Date    Progress
dokumen_1   1       11
dokumen_2   2       12
dokumen_3   3       13
dokumen_4   4       12
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading