英文:
FOR JSON PATH - produce array of numbers, not objects
问题
{
"ids": ["1", "2", "3"]
}
英文:
I have a SQL Server table with just one column which I am trying to convert to a .json
file.
The table is formatted like below
id |
---|
1 |
2 |
3 |
The desired JSON output looks like this:
{
"ids": [ "1", "2", "3" ]
}
The closest I have come is with the following code
SELECT
id
FROM
table
FOR JSON PATH, ROOT('ids')
Which returns
{
"ids": [ { "id": "1" },
{ "id": "2" },
{ "id": "3" }
]
}
Any help is appreciated
答案1
得分: 2
DECLARE @DataSource TABLE
(
[ID] INT
);
INSERT INTO @DataSource ([ID])
VALUES (1), (2), (3);
SELECT JSON_QUERY(CONCAT('[', STRING_AGG('"' + CAST(id AS VARCHAR) + '"', ','), ']')) AS ids
FROM @DataSource
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER;
英文:
Maybe something like this:
DECLARE @DataSource TABLE
(
[ID] INT
);
INSERT INTO @DataSource ([ID])
VALUES (1), (2), (3);
SELECT JSON_QUERY(CONCAT('[', STRING_AGG('"' + CAST(id AS VARCHAR) + '"', ','), ']')) AS ids
FROM @DataSource
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论