SQL OPENJSON Array in Objects

huangapple go评论63阅读模式
英文:

SQL OPENJSON Array in Objects

问题

以下是您需要的翻译:

I have a table with the following structure/rows:

ID OptionName OptionValue
0 Gender Male
1 Gender Female

我有一个具有以下结构/行的表格:

ID OptionName OptionValue
0 Gender Male
1 Gender Female

I want to query the database and return the following JSON:

我想查询数据库并返回以下JSON:

[{
"OptionName":"Gender",
"Values":[
"Male",
"Female"
]
}]

我想查询数据库并返回以下JSON:

[{
    "OptionName":"Gender",
    "Values":[
        "Male",
        "Female"
    ]
}]

However the result I'm currently getting is this:

但是我目前得到的结果是这样的:

[{
    "OptionName":"Gender",
    "Values":[
        {
            "OptionValue":"Male"
        },
        {
            "OptionValue":"Female"
        }
    ]
}]

Here is my Query:

这是我的查询:

SELECT TOP(1) OptionName,
(
	JSON_QUERY(
		(
			SELECT OptionValue 
			FROM [TestJSON].[dbo].[Options] 
			WHERE OptionName = 'Gender' 
			FOR JSON PATH
		)
	)
) AS [Values]
FROM [TestJSON].[dbo].[Options]
WHERE OptionName = 'Gender'
FOR JSON PATH

这是我的查询:

SELECT TOP(1) OptionName,
(
	JSON_QUERY(
		(
			SELECT OptionValue 
			FROM [TestJSON].[dbo].[Options] 
			WHERE OptionName = 'Gender' 
			FOR JSON PATH
		)
	)
) AS [Values]
FROM [TestJSON].[dbo].[Options]
WHERE OptionName = 'Gender'
FOR JSON PATH

What can I do to get the result I need?

我应该怎么做才能获得我需要的结果?

英文:

I have a table with the following structure/rows:

ID OptionName OptionValue
0 Gender Male
1 Gender Female

I want to query the database and return the following JSON:

[{
    "OptionName":"Gender",
    "Values":[
        "Male",
        "Female"
    ]
}]

However the result I'm currently getting is this:

[{
    "OptionName":"Gender",
    "Values":[
        {
            "OptionValue":"Male"
        },
        {
            "OptionValue":"Female"
        }
    ]
}]

Here is my Query:

SELECT TOP(1) OptionName,
(
	JSON_QUERY(
		(
			SELECT OptionValue 
			FROM [TestJSON].[dbo].[Options] 
			WHERE OptionName = 'Gender' 
			FOR JSON PATH
		)
	)
) AS [Values]
FROM [TestJSON].[dbo].[Options]
WHERE OptionName = 'Gender'
FOR JSON PATH

What can I do to get the result I need?

答案1

得分: 1

虽然 SQL Server 2022 引入了 JSON_ARRAY() 函数,但要使用它构建具有可变项目数量的 JSON 数组比较困难,因此您可以尝试基于字符串的方法:

SELECT DISTINCT o.OptionName, JSON_QUERY(a.[Values]) AS [Values]
FROM Options o
CROSS APPLY (
   SELECT CONCAT('[', STRING_AGG(CONCAT('"', OptionValue, '"'), ','), ']') 
   FROM Options
   WHERE OptionName = o.OptionName
) a ([Values]) 
--WHERE o.OptionName = 'Gender'
FOR JSON PATH

如果您知道每个选项的最大值数量(例如示例中的 5),还有另一种选择,可以使用 JSON_ARRAY()PIVOT 关系运算符的组合:

SELECT OptionName, JSON_ARRAY([1], [2], [3], [4], [5]) AS [Values]
FROM (  
   SELECT OptionName, OptionValue, ROW_NUMBER() OVER (PARTITION BY OptionName ORDER BY ID) AS Rn
   FROM Options
) t
PIVOT (MAX(OptionValue) FOR Rn IN ([1], [2], [3], [4], [5])) p
FOR JSON PATH
英文:

Although SQL Server 2022 introduced JSON_ARRAY() function, it's difficult to use it to build a JSON array with variable items count, so you may try a string based approach:

SELECT DISTINCT o.OptionName, JSON_QUERY(a.[Values]) AS [Values]
FROM Options o
CROSS APPLY (
   SELECT CONCAT('[', STRING_AGG(CONCAT('"', OptionValue, '"'), ','), ']') 
   FROM Options
   WHERE OptionName = o.OptionName
) a ([Values]) 
--WHERE o.OptionName = 'Gender'
FOR JSON PATH

If you know the maximum number of values for each option (5 in the example), a combination of JSON_ARRAY() and PIVOT relational operator is another option:

SELECT OptionName, JSON_ARRAY([1], [2], [3], [4], [5]) AS [Values]
FROM (  
   SELECT OptionName, OptionValue, ROW_NUMBER() OVER (PARTITION BY OptionName ORDER BY ID) AS Rn
   FROM Options
) t
PIVOT (MAX(OptionValue) FOR Rn IN ([1], [2], [3], [4], [5])) p
FOR JSON PATH

huangapple
  • 本文由 发表于 2023年2月8日 22:46:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定