0

I am using Newtonsoft to convert XML to JSON, and one of the API consumes JSON as input but using Newtonsoft I get different JSON result which is not compatible with the API.

<packages>
  <Package>
     <trackingNumber>23780498191986</trackingNumber>
     <shipCost>14</shipCost>
     <shipDate>2018-04-12T15:33:39.887</shipDate>
     <items>
        <Item>
           <dscoItemId>sdsd</dscoItemId>
           <sku>sdsd</sku>
           <quantity>1</quantity>
           <lineNumber>1</lineNumber>
           <originalLineNumber>1</originalLineNumber>
           <originalOrderQuantity>1</originalOrderQuantity>
        </Item>
        <Item>
           <dscoItemId>CCCVSD33</dscoItemId>
           <sku>TESRRST123</sku>
           <quantity>2</quantity>
           <lineNumber>2</lineNumber>
           <originalLineNumber>2</originalLineNumber>
           <originalOrderQuantity>2</originalOrderQuantity>
        </Item>
     </items>
     <shipCarrier />
     <shipMethod />
     <shippingServiceLevelCode>FESDPO</shippingServiceLevelCode>
     <numberOfLineItems>2</numberOfLineItems>
     <shipFrom>
        <attention>OPERATIONS m</attention>
        <firstName>SD</firstName>
        <lastName>SD</lastName>
        <company></company>
        <address1>ADD1</address1>
        <address2 />
        <city>URBANCREST</city>
        <region>OH</region>
        <postal>43123</postal>
        <country>US</country>
        <phone>123.125.1265</phone>
        <email />
        <locationCode />
     </shipFrom>
     <shipTo>
        <attention>SD</attention>
        <firstName>DD</firstName>
        <lastName>DD</lastName>
        <company>ABC CORPORATION</company>
        <address1>123 FINE WAY</address1>
        <address2>BUILDING F-1</address2>
        <city>BALTIMORE</city>
        <region>MD</region>
        <postal>21234</postal>
        <country>US</country>
        <phone>222-222-2222</phone>
        <email>XYZ@ACOMP.COM</email>
        <locationCode />
     </shipTo>
     <shipWeight>35.00</shipWeight>
  </Package>

Want to convert in below format for one of the API in my test app

"packages": [ 
{
  "trackingNumber": "7804981212191986",
  "shipCost": 14,
  "shipDate": "2018-04-12T15:33:39.887",
  "items": [
    {
      "dscoItemId": 10312120074055,
      "sku": "ABC11223",
      "quantity": 1,
      "lineNumber": 1,
      "originalLineNumber": 1,
      "originalOrderQuantity": 1
    }
  ],
  "shipCarrier": null,
  "shipMethod": null,
  "shippingServiceLevelCode": "FEPO",
  "numberOfLineItems": 1,
   "shipFrom": {
    "attention": "OPERATIONS manager",
    "firstName": "sd",
    "lastName": "Ysd",
    "company": "sd",
    "address1": "Add1",                
    "city": "URBANCREST",
    "region": "OH",
    "postal": "43123",
    "country": "US",
    "phone": "123.212.1235"        
  },
  "shipTo": {
    "attention": "Attention: GROUP A",
    "firstName": "MO",
    "lastName": "WK",
    "company": "ABC CORPORATION",
    "address1": "123 FINE WAY",
    "address2": "BUILDING F-1",
    "city": "BALTIMORE",
    "region": "MD",
    "postal": "21234",
    "country": "US",
    "phone": "222-222-2222",
    "email": "JOE@ACOMP.COM"        
  },
  "shipWeight": "35.00"
}
]

But using Newtonsoft I get below result which is invalid for API which I am consuming in one of test app.

"packages": {
"Package": {
  "trackingNumber": "780498193231986",
  "shipCost": "14.14",
  "shipDate": "2018-04-12T15:33:39.887",
  "items": {
    "Item": [
      {
        "dscoItemId": "AAVVCAACF",
        "sku": "ABC123",
        "quantity": "1",
        "lineNumber": "1",
        "originalLineNumber": "1",
        "originalOrderQuantity": "1"
      },
      {
        "dscoItemId": "CCCAAV33",
        "sku": "TEST123",
        "quantity": "2",
        "lineNumber": "2",
        "originalLineNumber": "2",
        "originalOrderQuantity": "2"
      }
    ]
  },
  "shipCarrier": null,
  "shipMethod": null,
  "shippingServiceLevelCode": "FEPO",
  "numberOfLineItems": "2",
  "shipFrom": {
    "attention": "OPERATIONS manager",
    "firstName": "asas",
    "lastName": "asas",
    "company": "asas",
    "address1": "Add1",
    "address2": null,
    "city": "URBANCREST",
    "region": "OH",
    "postal": "43123",
    "country": "US",
    "phone": "125.125.1235",
    "email": null,
    "locationCode": null
  },
  "shipTo": {
    "attention": "Attention: GROUP A",
    "firstName": "sa",
    "lastName": "sd",
    "company": "ABC CORPORATION",
    "address1": "123 FINE WAY",
    "address2": "BUILDING F-1",
    "city": "BALTIMORE",
    "region": "MD",
    "postal": "21234",
    "country": "US",
    "phone": "222-222-2222",
    "email": "JOE@ACOMP.COM",
    "locationCode": null
  }

Following code I am using for converting XML to JSON

private static string XmlToJSONWithJSONNet(String xmlDoc)
{
    string jsonString = string.Empty;
    XmlDocument xd = new XmlDocument();
    xd.LoadXml(Convert.ToString(xmlDoc));
    jsonString = JsonConvert.SerializeXmlNode(xd,Newtonsoft.Json.Formatting.None, true);
    return jsonString;
}
maccettura
  • 10,514
  • 3
  • 28
  • 35
Kuldeep Shige
  • 415
  • 3
  • 12
  • 24
  • You can try with [this](https://stackoverflow.com/questions/471946/how-to-convert-xml-to-json-in-python) solution, is in python – acoto Apr 18 '18 at 19:49
  • @acoto thats not very helpful is it? This is a question specifically tagged with JSON.NET – maccettura Apr 18 '18 at 19:51
  • You've presented a large amount of XML and JSON - could you reduce this to a much *smaller* example with just a few properties? That will make it much easier to compare the expected and actual results. – Jon Skeet Apr 18 '18 at 20:09

1 Answers1

1

I don't see a way to do this without transforming the XML. Basically, you'll need to get the XML into the format below.

One approach would be to deserialize the original XML into model A, copy the data into model B, and serialize model B into JSON.

Model classes:

public class ModelA
{
    public List<Package> Packages { get; set; }
    ...
}

public class ModelB
{
    public List<Package> Packages { get; set; }
    ...
}

Target XML:

<?xml version="1.0" encoding="UTF-8"?>
<manifest>
   <packages>
      <trackingNumber>23780498191986</trackingNumber>
      <shipCost>14</shipCost>
      <shipDate>2018-04-12T15:33:39.887</shipDate>
      <items>
         <dscoItemId>sdsd</dscoItemId>
         <sku>sdsd</sku>
         <quantity>1</quantity>
         <lineNumber>1</lineNumber>
         <originalLineNumber>1</originalLineNumber>
         <originalOrderQuantity>1</originalOrderQuantity>
      </items>
      <items>
         <dscoItemId>CCCVSD33</dscoItemId>
         <sku>TESRRST123</sku>
         <quantity>2</quantity>
         <lineNumber>2</lineNumber>
         <originalLineNumber>2</originalLineNumber>
         <originalOrderQuantity>2</originalOrderQuantity>
      </items>
      <shipCarrier />
      <shipMethod />
      <shippingServiceLevelCode>FESDPO</shippingServiceLevelCode>
      <numberOfLineItems>2</numberOfLineItems>
      <shipFrom>
         <attention>OPERATIONS m</attention>
         <firstName>SD</firstName>
         <lastName>SD</lastName>
         <company />
         <address1>ADD1</address1>
         <address2 />
         <city>URBANCREST</city>
         <region>OH</region>
         <postal>43123</postal>
         <country>US</country>
         <phone>123.125.1265</phone>
         <email />
         <locationCode />
      </shipFrom>
      <shipTo>
         <attention>SD</attention>
         <firstName>DD</firstName>
         <lastName>DD</lastName>
         <company>ABC CORPORATION</company>
         <address1>123 FINE WAY</address1>
         <address2>BUILDING F-1</address2>
         <city>BALTIMORE</city>
         <region>MD</region>
         <postal>21234</postal>
         <country>US</country>
         <phone>222-222-2222</phone>
         <email>XYZ@ACOMP.COM</email>
         <locationCode />
      </shipTo>
      <shipWeight>35.00</shipWeight>
   </packages>
</manifest>
Dan Wilson
  • 3,937
  • 2
  • 17
  • 27