我想删除"AttributeName" : "Manufacturer"
来自 SQL Server 2016 中的以下 json:
declare @json nvarchar(max) = '[{"Type":"G","GroupBy":[],
"Attributes":[{"AttributeName":"Class Designation / Compressive Strength"},{"AttributeName":"Size"},{"AttributeName":"Manufacturer"}]}]'
这是我尝试过的查询,但不起作用
select JSON_MODIFY((
select JSON_Query(@json, '$[0].Attributes') as res),'$.AttributeName.Manufacturer', null)
这是使用的工作解决方案for json and 打开 json。重点是:
确定您要删除的项目并将其替换为NULL
。这是由JSON_MODIFY(@json,'$[0].Attributes[2]', null)
。我们只是说,将第二个元素放入Attributes
并将其替换为 null
将此数组转换为行集。我们需要以某种方式摆脱这个null
元素,这是我们可以在 SQL 中轻松过滤的内容where [value] is not null
将其全部组装回原始 JSON。这是由FOR JSON AUTO
请记住此类 JSON 数据转换的一个重要方面:
JSON 设计用于信息交换或最终存储信息。但您应该避免 SQL 级别上更复杂的数据操作。
无论如何,解决方案在这里:
declare @json nvarchar(max) = '[{"Type": "G","GroupBy": [],"Attributes": [{"AttributeName": "Class Designation / Compressive Strength"}, {"AttributeName": "Size"}, {"AttributeName": "Manufacturer"}]}]';
with src as
(
SELECT * FROM OPENJSON(
JSON_Query(
JSON_MODIFY(@json,'$[0].Attributes[2]', null) , '$[0].Attributes'))
)
select JSON_MODIFY(@json,'$[0].Attributes', (
select JSON_VALUE([value], '$.AttributeName') as [AttributeName] from src
where [value] is not null
FOR JSON AUTO
))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)