0

I am facing an issue in converting XML to JSON

My input xml is

<Message>
    <Sequence id="S">
        <Tag id="T1"></Tag>
        <Sequence id="S1">
            <Tag id="T2"></Tag>
        </Sequence>
        <Tag id="T3"></Tag>
    </Sequence>
</Message>

I am using NewtonSoft JsonConvert to convert it to Json

The converted Json is like this, all elements with name 'Tag' are grouped into one array, They should be grouped into multiple seperate 'Tag' arrays at same level

"Message": {
    "Sequence": {
      "@id": "S",
      "Tag": [
        {
          "@id": "T1"
        },
        {
          "@id": "T3"
        }
      ],
      "Sequence": {
        "@id": "S1",
        "Tag": {
          "@id": "T2"
        }
      }
    }
  }

I am expecting the conversion to be

"Message": {
    "Sequence": {
      "@id": "S",
      "Tag": [
        {
          "@id": "T1"
        }
      ],
      "Sequence": {
        "@id": "S1",
        "Tag": {
          "@id": "T2"
        }
      },
      "Tag": [
        {
          "@id": "T3"
        }
      ]
    }
  }

Please let me know your thoughts on how to fix this

saikumar s
  • 11
  • 1
  • https://www.newtonsoft.com/json/help/html/JsonPropertyOrder.htm – xdtTransform Jan 02 '19 at 07:08
  • 1
    Possible duplicate of [Order of serialized fields using JSON.NET](https://stackoverflow.com/questions/3330989/order-of-serialized-fields-using-json-net) – xdtTransform Jan 02 '19 at 07:08
  • 1
    could you please show your code for converting xml to json? – er-sho Jan 02 '19 at 07:09
  • I am using the below code to convert xml to json string XMLToJson(string xml) { XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); return JsonConvert.SerializeXmlNode(doc); } – saikumar s Jan 02 '19 at 07:11
  • 3
    your expected json isn't valid, properties cannot be duplicated – Alex Riabov Jan 02 '19 at 07:33
  • Alex Riabov: I need to create an Html UI form from the xml, order of the elements should be same – saikumar s Jan 02 '19 at 07:55
  • Your xml is nested so Sequence has a child element that is also Sequence. That is why you are not getting the expected results. – jdweng Jan 02 '19 at 08:15
  • 1
    Json.NET is working as documented. You are expecting Json.NET to create duplicate JSON property names during XML to JSON conversion, but it will not do that. The [documentation](https://www.newtonsoft.com/json/help/html/ConvertingJSONandXML.htm) states, *Multiple nodes with the same name at the same level are grouped together into an array.* which is exactly what you are seeing. Also, [rfc 8259](https://tools.ietf.org/html/rfc8259) states, *The names within an object SHOULD be unique.* so your preference for duplicated names is almost certain to be atypical. – dbc Jan 02 '19 at 17:53
  • Does your XML have a fixed schema? – dbc Jan 02 '19 at 19:04
  • If you want the `"Tag"` properties to be duplicated, why would their values be an array of objects containing one object, rather than a single object? – dbc Jan 02 '19 at 19:09

1 Answers1

0

The XML and the JSON result is quiet confusing.
but if we analyze them it will be much clear to explain the result:

Here is your XML hierarchy, each "level" has its own color:

enter image description here

  1. <Message> has <Sequence> child object (S) that holds 2 <Tag> objects - they are 2 (2 = multiple..) objects and they are both of "type" - <Tag> - That is why you will get an array in your JSON result.

  2. inside your <Sequence> child you have a nested <Sequence> object, this object is a child relative to the first <Sequence> object.
    IF the two of them where both children of <Message> then the JSON result would also be an array of them ("Sequence": [...],).
    but in this case S1 is grandchild relative to <message> and child relative to S.

  3. Now, back to the second <Sequence> object - it has only one <tag> (T2) that is why it is not inside an array and you can see it is nested to the first <Sequence> object (S):

enter image description here

You can see it more clearly in XML to JSON converter at CodVerter.com and validate the JSON result + make it more readable in JSON Validator tool.


Here is the JSON Result (from CodVerter`s XML to JSON tool):

enter image description here

Jonathan Applebaum
  • 5,738
  • 4
  • 33
  • 52