Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert JSON array to XML

I'm trying to convert JSON to XML. My JSON contains an array of cars and each car has an array of features:

[
    {
        "car": {
            "features": [{
                "code": "1"
            }, {
                "code": "2"
            }]
        }
    },
    {
        "car": {
            "features": [{
                "code": "3"
            }, {
                "code": "2"
            }]
        }
    }
]

I'm converting this to XML:

// the tag name for each top level element in the json array
var wrappedDocument = string.Format("{{ car: {0} }}", jsonResult);
// set the root tag name
return JsonConvert.DeserializeXmlNode(wrappedDocument, "cars");

This is the resulting XML:

<cars>
   <car>
      <features>
        <code>1</code>
      </features>
      <features>
        <code>2</code>
      </features>
   </car>
   <car>
      <features>
        <code>3</code>
      </features>
      <features>
        <code>2</code>
      </features>
   </car>
</cars>

My problem is that I would like to have all "features" listed under a common element just like "car" is listed under "cars" so that the XML would look like this:

<cars>
   <car>
       <features>
          <feature>
            <code>1</code>
          </feature>
          <feature>
            <code>2</code>
          </feature>
        </features>
   </car>
   <car>
       <features>
          <feature>
            <code>3</code>
          </feature>
          <feature>
            <code>2</code>
          </feature>
        </features>
   </car>
</cars>

Is that possible using Newtonsoft Json.NET? Thank you for any help!

like image 639
peter Avatar asked Mar 17 '16 12:03

peter


1 Answers

DeserializeXmlNode() doesn't really have a way to customize the way it does its JSON to XML conversion. To get the result you want using that method, you will either have to manipulate the JSON before converting it to XML, or manipulate the XML afterwards.

In this case, I think it might be easier to use Json.Net's LINQ-to-JSON API to build the XML directly from the JSON in the shape you want. You can do it like this:

var ja = JArray.Parse(jsonResult);
var xml = new XDocument(
    new XElement("cars", 
        ja.Select(c => 
            new XElement("car",
                new XElement("features", 
                    c["car"]["features"].Select(f => 
                        new XElement("feature", 
                            new XElement("code", (string)f["code"])
                        )
                    )
                )
            )
        )
    )
);

Console.WriteLine(xml.ToString());

Fiddle: https://dotnetfiddle.net/fxxQnL

like image 102
Brian Rogers Avatar answered Sep 26 '22 06:09

Brian Rogers