I want to delete duplicate nodes from JSON in a column of a SQL Server table.
JSON column:
{
"Categories": [
{
"Type": "Type1",
"GDS": [
{
"GDSName": "Type1_test1",
"IsEnable": true,
"Priority": 2
},
{
"GDSName": "Type1_test2",
"IsEnable": false,
"Priority": 0
}
]
},
{
"Type": "Type2",
"GDS": [
{
"GDSName": "Type2_test1",
"IsEnable": false,
"Priority": 0
},
{
"GDSName": "Type2_test2",
"IsEnable": true,
"Priority": 0
},
{
"GDSName": "Type2_test3",//this is duplicate,keep this
"IsEnable": true,
"Priority": 0
},
{
"GDSName": "Type2_test3",//this is duplicate ,delete this
"IsEnable": true,
"Priority": 0
}
]
},
{
"Type": "Type3",
"GDS": [
{
"GDSName": "Type3_test3",
"IsEnable": true,
"Priority": 0
}
]
}
]
}
Here in
"Type": "Type2",
"GDS [2] & [3] of GDSName": "Type2_test3"
are duplicates.
I need to make sure If Type2_test3 is present and it is multiple then delete duplicate entries by keeping Type2_test3 single entry per column.
1条答案
按热度按时间aurhwmvo1#
One possible approach to remove the duplicates are the following steps:
Categories
JSON arrays from the input JSON as table usingOPENJSON()
with explicit schemaFOR JSON AUTO
Categories
JSON arrays with the new JSON usingJSON_MODIFY()
:Of course, you need at least SQL Server 2016 to use the build-in JSON support.
JSON:
Statement: