ChoETL icon indicating copy to clipboard operation
ChoETL copied to clipboard

ChoXmlWriter - Array item names

Open k-schneider opened this issue 1 year ago • 1 comments

Hello,

Is there a way to customize the array item names produced when converting from JSON to XML?

For example:

var json = @"
[
    {
        ""name"": ""John"",
        ""age"": 30,
        ""address"": {
            ""city"": ""New York"",
            ""street"": ""5th Avenue"",
            ""number"": 123
        },
        ""phones"": [
            {
                ""type"": ""Home"",
                ""number"": ""123-456-7890""
            },
            {
                ""type"": ""Work"",
                ""number"": ""456-789-0123""
            }
        ]
    }
]";

var sb = new StringBuilder();
using (var xml = new ChoXmlWriter(sb).Configure(x =>
{
    x.RootName = "root";
    x.NodeName = "item";
    x.UseXmlArray = true;
    x.DoNotEmitXmlNamespace = true;
    x.ThrowAndStopOnMissingField = false;
}))
using (var r = new ChoJSONReader(JToken.Parse(json)))
{
    xml.Write(r);
}

Console.WriteLine(sb.ToString());

Produces:

<root>
  <item>
    <name>John</name>
    <age>30</age>
    <address>
      <city>New York</city>
      <street>5th Avenue</street>
      <number>123</number>
    </address>
    <phones>
      <phone>
        <type>Home</type>
        <number>123-456-7890</number>
      </phone>
      <phone>
        <type>Work</type>
        <number>456-789-0123</number>
      </phone>
    </phones>
  </item>
</root>

But I need the individual phone nodes to have the tag name "item" like this:

<root>
  <item>
    <name>John</name>
    <age>30</age>
    <address>
      <city>New York</city>
      <street>5th Avenue</street>
      <number>123</number>
    </address>
    <phones>
      <item>
        <type>Home</type>
        <number>123-456-7890</number>
      </item>
      <item>
        <type>Work</type>
        <number>456-789-0123</number>
      </item>
    </phones>
  </item>
</root>

This is just a simple example but I am trying to generically handle any json structure so I can't hardcode any rules or assume the shape of the data.

Thanks!

k-schneider avatar Apr 15 '24 15:04 k-schneider

Here is how you can accomblish ur need

            var sb = new StringBuilder();
            using (var xml = new ChoXmlWriter(sb).Configure(x =>
            {
                x.RootName = "root";
                x.NodeName = "item";
                x.UseXmlArray = true;
                x.DoNotEmitXmlNamespace = true;
                x.ThrowAndStopOnMissingField = false;
            }))
            {
                using (var r = new ChoJSONReader(JToken.Parse(json)))
                {
                    var recs = r.ToArray();
                    foreach (var rec in recs)
                    {
                        foreach (var phone in rec.phones) // Rename 'phone' to 'item'
                        {
                            phone.DynamicObjectName = "item";
                        }
                    }
                    xml.Write(recs);
                }
            }

Cinchoo avatar Jun 06 '25 01:06 Cinchoo