I am trying to convert xml to json and running into an issue where one element is appearing as an array and I don't want it to. Here's sample data and ddl.
Here are the results I am getting. You can see the array at RemoveBracketHere. I want to remove this bracket and corresponding close bracket. Any help would be appreciated. TIA
[
{
"id": 1,
"templateContent": {
"RemoveBracketHere": [
{
"userInfo": {
"userID": "DummyUserID"
},
"column": [
{
"excelColumn": "Dept",
"StdColumn": "Department Number"
},
{
"excelColumn": "Season",
"StdColumn": "Season"
},
{
"excelColumn": "Class",
"StdColumn": "Class"
}
]
}
]
}
}
]
drop table if exists #t
go
create table #t (Division int not null primary key, x xml)
insert into #t values (1, '<Columns>
<UserInfo>
<UserID>DummyUserID</UserID>
</UserInfo>
<Column>
<ExcelColumn>Dept</ExcelColumn>
<StdColumn>Department Number</StdColumn>
</Column>
<Column>
<ExcelColumn>Season</ExcelColumn>
<StdColumn>Season</StdColumn>
</Column>
<Column>
<ExcelColumn>Class</ExcelColumn>
<StdColumn>Class</StdColumn>
</Column>
</Columns>')
select (
Select Division as id,
(select x.value('(//Columns/UserInfo/UserID)[1]', 'varchar(30)') AS 'userInfo.userID',
(
select
coldata.value('(ExcelColumn)[1]', 'varchar(30)') AS 'excelColumn'
,coldata.value('(StdColumn)[1]', 'varchar(30)') AS 'StdColumn'
FROM #t d
cross apply x.nodes('//Columns/Column') as xmldata(coldata)
where d.Division = d1.Division
FOR json path) as 'column'
from #t d1
where d1.Division = dim.Division
for json path) 'templateContent.WantSquigglyBracketNotBracketHere'
from #t Dim
for json path
) as json