I have the following table:
Test_ID | Test_JSON |
---|---|
some uniqueidentifer | see below |
some uniqueidentifier | see below |
The Test_JSON structure is as follows:
{
"LaneIds": {
"1": "a5a8ff96-0291-4ef9-890a-57c1874398bc",
"2": "7a698a8b-934b-4a57-99c7-29a200d87877",
"3": "aa30906d-e859-40a8-aedd-ae8ae10d0ae1",
"4": "ccb05b71-a65a-4db7-b7c9-b4b7251150c7",
"5": "c10593a9-06c3-49e4-bf4e-9d1c5ece98d4",
"6": "6eeb9c85-f3ac-44cb-be74-f2d7554ea8d3",
"7": "1226abb3-58c3-4b1e-844f-f2643d874490",
"8": "7256ce43-014a-4dd6-a072-f8de2c373688"
},
"SomeOtherID": "b9d75ef9-b68d-46ef-9d34-3f5e99f448e2",
"SomeOtherProperty": 12345
}
I need to get all lane IDs in a comma separated list for all Test_IDs. So my final output needs to be like this:
Test_ID | Column B |
---|---|
some uniqueidentifier | "a5a8ff96-0291-4ef9-890a-57c1874398bc, 7a698a8b-934b-4a57-99c7-29a200d87877, aa30906d-e859-40a8-aedd-ae8ae10d0ae1, etc." |
next test_id | lane_ids |
What is the best way to do this using SQL/T-SQL?
I am able to get the LaneIds using JSON_QUERY() but I specifically need to drop the ints in the key field and only return the GUIDs in a comma separated list.
2
Answers
Since you mentioned you’re already using
JSON_QUERY()
, I’m guessing the problem is with the definition of your JSON path. Have you tried the following:JSON_QUERY(Test_JSON, '$.LaneIds[*]')
? This would query the column you showed as JSON, look for the keyLaneIds
and get all values from within that key.This should get your values in all their denormalized goodness: