Issues of Converting XML to JSON

Q

What are main issues of converting XML to JSON?

✍: FYIcenter.com

A

Here are main issues of converting an XML document to a JSON text string:

1. Differentiation of attributes and sub elements - An XML Element may have 2 types of named sub structures: attributes and sub elements. If we want to convert both XML attributes and sub elements to JSON object properties, how do we differentiate them in the resulting JSON object?

For example, the following XML element "ul" has an attribute "class" and a sub element "li":

<ul class="footNotes">
  <li>Price may change without prior notice.</li>
</ul>

Many conversion tools decide to prefix attribute names with "@" when converting them to JSON properties, as shown below:

{
   "ul": {
      "@class": "footNotes",
      "li": "Price may change without prior notice."
   }
}

2. Mixed content of text and sub element - An XML Element may have a mixed content of a text string and a sub element. If we want to convert the text string and the sub element to properties, how do we name the text string property?

For example, the following XML element "p" has a text string "Summer Special: " and a sub element "b":

<p>Summer Special: <b>20% Off on everything!</b>
</p>

Many conversion tools decide to name the property converted from text content as "#text" as shown below:

{
  "p": {
    "#text": "Summer Special: ",
    "b": "20% Off on everything!"
  }
}

3. Scattered text contents - An XML Element may have multiple text contents scattered between sub elements. If we want to convert those text contents together as single "#text" property, their locations relative to sub elements are lost in the conversion.

For example, the following XML element "p" has 2 text contents of "Welcome to " and " Website!" located before and after the "b" sub element:

<p>Welcome to <b>FYIcenter.com</b> Website!</p>

Many conversion tools decide to aggregate scattered text contents into a single property named as "#text", with the property value set to an array to hold their contents as shown below:

{
  "p": {
    "#text": [
      "Welcome to ",
      " Website!"
    ],
    "b": "FYIcenter.com"
  }
}

4. Repeating sub elements - An XML Element may have repeating sub elements, where multiple sub elements have the identical element name. If we want to convert those sub elements to properties, their property names will be same, which is not allowed in JSON.

For example, the following XML element "ul" has 3 repeating sub elements named as "li":

<ol>
  <li>Scope</li>
  <li>Time</li>
  <li>Cost</li>
</ol>

Many conversion tools decide to aggregate repeating sub elements into a single property, with the property value set to an array to hold their contents as shown below:

{
  "ol": {
    "li": [
      "Scope",
      "Time",
      "Cost"
    ]
  }
}

5. Scattered repeating sub elements - An XML Element may have scattered repeating sub elements, where multiple sub elements of the identical element name scattered among other sub elements. If we want to aggregate those sub elements to a single property with an array as the value, their locations relative to other sub elements are lost in the conversion.

For example, the following XML element "body" has 2 repeating sub elements named as "p" and 2 repeating sub elements named as "blockquote":

<body>
  <p>Username:</p>
  <blockquote>nobody</blockquote>
  <p>Password:</p>
  <blockquote>noidea</blockquote>
</body>

Many conversion tools decide to aggregate sub elements in the above example into 2 properties: "p" and "blockquote", and each holds an array for their contents respectively as shown below:

{
  "body": {
    "p": [
      "Username:",
      "Password:"
    ],
    "blockquote": [
      "nobody",
      "noidea"
    ]
  }
}

 

Mapping XML Attributes to JSON Values

XML to JSON Conversion

XML to JSON Conversion

⇑⇑ JSON Tutorials

2023-07-11, 1367🔥, 0💬