skip to Main Content

I am trying to convert an XML to JSON without changing the datatypes but it is always returning the string value. I have tried different ways to convert with proper data types.
Below is my source XML and expected JSON

<Employee >
  <Name>John</Name>
  <Age>18</Age>
  <IsContractor>true</IsContractor>
  <Salary>5555.66</Salary>
</Employee>

Expected Result:

 "Employee": {
      "Name": "John",
      "Age": 18,
      "IsContractor": true,
      "Salary": 5555.66
    }

I have tried different ways but no luck

 string xml = @"<Employee>
                            <Name>John</Name>
                            <Age json:Type='Integer'>18</Age>
                            <IsContractor json:Type='Boolean'>true</IsContractor>
                            <Salary json:Type='Decimal'>5555.66</Salary>
                        </Employee>";
      
        var doc = new XmlDocument();
        doc.LoadXml(xml);
        var result = JsonConvert.SerializeXmlNode(doc);

Also, I tried adding a datatype like this

          var xml = @"<Employee  xmlns:m=""urn:informatica:ae:xquery:json2xml:meta-data"">
                    <Name>John</Name>
                    <Age m:type=""xs:double"">18</Age>
                    <IsContractor m:type=""xs:boolean"">true</IsContractor>
                     </Employee>";
        var doc = new XmlDocument();
        doc.LoadXml(xml);
        var result1 = JsonConvert.SerializeXmlNode(doc);

But both ways all the values are converted to a string.

2

Answers


  1. You could use a workaround that utilizes Newtonsoft.Json to convert XML to JSON and then parse the JSON again to convert the string values to their appropriate data types.

    string xml = @"<Employee>
                    <Name>John</Name>
                    <Age>18</Age>
                    <IsContractor>true</IsContractor>
                    <Salary>5555.66</Salary>
                    </Employee>";
    
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    
    string jsonText = JsonConvert.SerializeXmlNode(doc);
    JObject json = JObject.Parse(jsonText);
    
    // parse values to appropriate data types
    json["Employee"]["Age"] = int.Parse(json["Employee"]["Age"].ToString());
    json["Employee"]["IsContractor"] = bool.Parse(json["Employee"]["IsContractor"].ToString());
    json["Employee"]["Salary"] = decimal.Parse(json["Employee"]["Salary"].ToString());
    
    Console.WriteLine(json.ToString());
    
    Login or Signup to reply.
  2. This code works for me. If you have nested objects for example you just need to change an iteration algorithm

    var xml = @"
    <Employee >
      <Name>John</Name>
      <Age type='System.Int32'>18</Age>
      <IsContractor type='System.Boolean'>true</IsContractor>
      <Salary type='System.Double'>5555.66</Salary>
    </Employee>";
    
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xml);
    
        string json = JsonConvert.SerializeXmlNode(xmlDoc, Newtonsoft.Json.Formatting.None, false);
        var jObj = JObject.Parse(json);
    
        foreach (JObject element in jObj.Properties().First())
        {
            foreach (var prop in element.Properties())
            {
                if (prop.Value.Type == JTokenType.Object)
                    prop.Value = ConvertToType((JObject)prop.Value);
            }
        }
    
        json = jObj.ToString();
    
    public JValue ConvertToType(JObject jObj)
    {
        var val = Convert.ChangeType(jObj["#text"], Type.GetType((string)jObj["@type"]));
        return new JValue(val);
    }
    

    output

    {
      "Employee": {
        "Name": "John",
        "Age": 18,
        "IsContractor": true,
        "Salary": 5555.66
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search