I want to convert a XML to JSON. But due to the namespace,prefix and json array issues I am facing few issues.
Input XML
<notifications xmlns="http://soap.sforce.com/2005/09/outbound">
<OrganizationId>123</OrganizationId>
<ActionId>123</ActionId>
<SessionId xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<EnterpriseUrl>qwe</EnterpriseUrl>
<PartnerUrl>qwe</PartnerUrl>
<Notification>
<Id>123</Id>
<sObject xsi:type="sf:Opportunity" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
<sf:Id>ao123</sf:Id>
<sf:Amount>60000.0</sf:Amount>
<sf:CreatedDate>2014-11-26T14:45:52.000Z</sf:CreatedDate>
<sf:IsClosed>false</sf:IsClosed>
</sObject>
</Notification>
</notifications>
Output JSON
{
"notifications": {
"OrganizationId": "123",
"ActionId": "123",
"SessionId": {
"@nil": "true"
},
"EnterpriseUrl": "qwe",
"PartnerUrl": "qwe",
"Notification": [
{
"Id": "ao123",
"sObject": {
"@type": "sf:Opportunity",
"Id": "ao123",
"Amount": "60000.0",
"CreatedDate": "2014-11-26T14:45:52.000Z",
"IsClosed": "false"
}
}
]
}
}
So below are few issues which I am facing
- Namespace and prefix of XML should not appear in json.
Notification
should be a json array even if I receive one item
So what I have tried so far is removing namespace and prefix using this method and then converting it to JSON using JsonConvert.SerializeXNode
. Also to handle the array I can add json:Array="true"
as mentioned here
I feel these steps are more of data manipulation and I am looking for some good approaches to achieve same. So I have tried using XSLT and I am able to remove the namespace prefix. fiddle link for XSLT. But I am not sure how to use XSLT to remove prefix and then convert my XML to my expected JSON format(may be using the XSLT xml-to-json options). Looking for a solution for this using XSLT