-1

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

techresearch
  • 119
  • 3
  • 14

1 Answers1

1

you can try try this code

var xDoc = XDocument.Parse(xmlString);

xDoc.Root.DescendantNodesAndSelf().OfType<XElement>().Attributes().Where(att => att.IsNamespaceDeclaration).Remove();

xDoc.Root.DescendantNodesAndSelf().OfType<XElement>().ToList().ForEach(node => node.Name = node.Name.LocalName);

var json = JsonConvert.SerializeXNode(xDoc, Newtonsoft.Json.Formatting.Indented, false);

or assuming https://xsltfiddle.liberty-development.net/aUPRNo/1 that after XSLT your xml is

<?xml version="1.0" encoding="UTF-8"?>
<notifications>
   <OrganizationId>123</OrganizationId>
   <ActionId>123</ActionId>
   <SessionId nil="true" />
   <EnterpriseUrl>qwe</EnterpriseUrl>
   <PartnerUrl>qwe</PartnerUrl>
   <Notification>
      <Id>123</Id>
      <sObject type="sf:Opportunity">
         <Id>ao123</Id>
         <Amount>60000.0</Amount>
         <CreatedDate>2014-11-26T14:45:52.000Z</CreatedDate>
         <IsClosed>false</IsClosed>
      </sObject>
   </Notification>
</notifications>
<notifications>
   <OrganizationId>123</OrganizationId>
   <ActionId>123</ActionId>
   <SessionId nil="true" />
   <EnterpriseUrl>qwe</EnterpriseUrl>
   <PartnerUrl>qwe</PartnerUrl>
   <Notification>
      <Id>123</Id>
      <sObject type="sf:Opportunity">
         <Id>ao123</Id>
         <Amount>60000.0</Amount>
         <CreatedDate>2014-11-26T14:45:52.000Z</CreatedDate>
         <IsClosed>false</IsClosed>
      </sObject>
   </Notification>
</notifications>

you can use this code to convert it to json

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
    
XmlNode node = xmlDoc.SelectSingleNode("notifications");

var json =  JsonConvert.SerializeXmlNode(node, Newtonsoft.Json.Formatting.Indented, false);

in both cases the output is the same

{
 "notifications": {
  "OrganizationId": "123",
  "ActionId": "123",
  "SessionId": {
    "@nil": "true"
  },
  "EnterpriseUrl": "qwe",
  "PartnerUrl": "qwe",
  "Notification": {
    "Id": "123",
    "sObject": {
      "@type": "sf:Opportunity",
      "Id": "ao123",
      "Amount": "60000.0",
      "CreatedDate": "2014-11-26T14:45:52.000Z",
      "IsClosed": "false"
    }
  }
}
}
Serge
  • 40,935
  • 4
  • 18
  • 45