在Multi-Objects JSON数组中,我想修改对象的所有值,并且我很难访问这些值,即
实施例 -
DECLARE @json nvarchar(MAX)
SET @json = N'[{"name": "John","sex": "F"}, {"name": "Jane","sex": "F"}]'
我想将所有对象的性别值修改为“M”。
JSON_MODIFY(@json, '$.Sex', 'M')
无效。有什么方法可以解决我的问题吗?
答案 0 :(得分:1)
这应该有帮助
DECLARE @jsonstr NVARCHAR(MAX) = '[{"name": "John","sex": "F"}, {"name": "Jane","sex": "F"}]}'
;WITH CTE AS
(
SELECT * FROM OPENJSON(@jsonstr)
WITH ([name] VARCHAR(100) '$.name' , [sex] VARCHAR(100) '$.sex' )
)
,CTE1 AS
(
SELECT [name], case when [sex] = 'F' THEN 'M' ELSE [sex] END [sex]
FROM CTE
)
SELECT *
FROM CTE1
FOR JSON AUTO
GO
<强>输出强>
[{"name":"John","sex":"M"},{"name":"Jane","sex":"M"}]