I'm trying to figure out the appropriate syntax to query the Note column from only the "1" object under "worklistData"...and failing miserably. I've gone through several iterations, but the code below seems to be what "should" be working (but clearly isn't). The query's good up until the last CROSS APPLY, then it's kicking back the error:
"JSON path is not properly formatted. Unexpected character '1' is found at position 2."
Would one of you be so kind as to point out where I'm screwing this up?
DECLARE @JSON VARCHAR(MAX) =
'
{
"_id":143884,
"member":{
"memberId":"12345",
"firstName":"Test",
"lastName":"Test"
},
"worklistData":{
"1":{
"Note":"Note 1 text goes here",
"thUpdateCount":26
},
"2":{
"Note":"This is a test",
"thUpdateCount":1
}
}
}
';
SELECT RT.*, MEM.*, WL1.*
FROM OPENJSON(@JSON)
WITH (
SK VARCHAR(MAX) '$._id'
) RT
CROSS APPLY OPENJSON(@JSON, '$.member')
WITH (
SourceMemberID VARCHAR(MAX) '$.memberId',
FirstName VARCHAR(MAX) '$.firstName',
LastName VARCHAR(MAX) '$.lastName'
)MEM
CROSS APPLY OPENJSON(@JSON, '$.worklistData') WL
CROSS APPLY OPENJSON(WL.value, '$.1')
WITH (
Note1 VARCHAR(MAX) '$.Note'
) WL1;
Thanks in advance!