Suppose I have this SQL working:
SELECT(
SELECT
a.id,
a.label,
a.text
FROM [data] a
FOR JSON PATH, INCLUDE_NULL_VALUES
) AS 'data'
which will produce JSON with this structure:
[
{
"id": "N/A",
"label": "test",
"text": "Not applicable"
},
{
"id": "ID",
"label": "test2",
"text": "Identification"
}
]
What I would like to have is this structure – the value of the label column used as key.
[{
"test": {
"id": "N/A",
"text": "Not applicable"
},
"test2": {
"id": "ID",
"text": "Identification"
}
}]
Is it possible, with SQL (not building the JSON by strings)?
2
Answers
You could use FOR XML to concatenate/aggregate the FOR JSON PATH of id and text
Unfortunately, SQL Server does not yet support
JSON_OBJECT_AGG
, which would have worked well for this.You can instead use
STRING_AGG
andSTRING_ESCAPE
, with a nestedFOR JSON
subquery.This works for SQL Server 2017+.