← Back to Blog

XML to JSON Conversion: Avoiding Data Loss

April 8, 2026 3 min read By CodeTidy Team

The XML to JSON Conversion Conundrum

We've all been there - staring at a complex XML document, wishing it were a simple JSON object. But when converting between these two data formats, it's easy to lose crucial data or end up with a mess that's harder to work with than the original XML. In this post, we'll explore the common pitfalls of XML to JSON conversion and provide practical advice on how to avoid them.

Table of Contents

  • Understanding the Challenges of XML to JSON Conversion
  • Handling Attributes and Mixed Content
  • Dealing with Namespaces and XML Schemas
  • Arrays vs Objects: Choosing the Right Data Structure
  • Conventions for XML to JSON Mapping
  • Key Takeaways
  • FAQ

Understanding the Challenges of XML to JSON Conversion

When converting XML to JSON, we often encounter issues with attributes, mixed content, and namespaces. Let's take a look at an example:

<person>
  <name>John Doe</name>
  <address>
    <street>123 Main St</street>
    <city>Anytown</city>
    <state>CA</state>
    <zip>12345</zip>
  </address>
</person>

A naive conversion might result in the following JSON:

{
  "person": {
    "name": "John Doe",
    "address": {
      "street": "123 Main St",
      "city": "Anytown",
      "state": "CA",
      "zip": "12345"
    }
  }
}

But what about attributes? What if our XML looked like this:

<person id="123">
  <name>John Doe</name>
  <address>
    <street>123 Main St</street>
    <city>Anytown</city>
    <state>CA</state>
    <zip>12345</zip>
  </address>
</person>

We'd want our JSON to reflect the id attribute, but how?

Handling Attributes and Mixed Content

One approach is to use a convention like Badgerfish, which treats attributes as nested objects:

{
  "person": {
    "$": {
      "id": "123"
    },
    "name": "John Doe",
    "address": {
      "street": "123 Main St",
      "city": "Anytown",
      "state": "CA",
      "zip": "12345"
    }
  }
}

Alternatively, we could use a convention like Parker, which treats attributes as key-value pairs at the same level as the element's content:

{
  "person": {
    "id": "123",
    "name": "John Doe",
    "address": {
      "street": "123 Main St",
      "city": "Anytown",
      "state": "CA",
      "zip": "12345"
    }
  }
}

We recommend using the Parker convention, as it results in a more straightforward and intuitive JSON structure.

Dealing with Namespaces and XML Schemas

XML namespaces can add an extra layer of complexity to our conversion. Let's consider an example:

<root xmlns:ns="http://example.com/ns">
  <ns:person>
    <ns:name>John Doe</ns:name>
    <ns:address>
      <ns:street>123 Main St</ns:street>
      <ns:city>Anytown</ns:city>
      <ns:state>CA</ns:state>
      <ns:zip>12345</ns:zip>
    </ns:address>
  </ns:person>
</root>

To handle namespaces, we can use a convention like GData, which prefixes namespace-qualified elements with the namespace URI:

{
  "http://example.com/ns": {
    "person": {
      "name": "John Doe",
      "address": {
        "street": "123 Main St",
        "city": "Anytown",
        "state": "CA",
        "zip": "12345"
      }
    }
  }
}

Arrays vs Objects: Choosing the Right Data Structure

When converting XML to JSON, we often need to decide whether to represent a collection of elements as an array or an object. Let's consider an example:

<people>
  <person>
    <name>John Doe</name>
    <address>
      <street>123 Main St</street>
      <city>Anytown</city>
      <state>CA</state>
      <zip>12345</zip>
    </address>
  </person>
  <person>
    <name>Jane Doe</name>
    <address>
      <street>456 Elm St</street>
      <city>Othertown</city>
      <state>NY</state>
      <zip>67890</zip>
    </address>
  </person>
</people>

A possible JSON representation might be:

{
  "people": [
    {
      "name": "John Doe",
      "address": {
        "street": "123 Main St",
        "city": "Anytown",
        "state": "CA",
        "zip": "12345"
      }
    },
    {
      "name": "Jane Doe",
      "address": {
        "street": "456 Elm St",
        "city": "Othertown",
        "state": "NY",
        "zip": "67890"
      }
    }
  ]
}

However, if the order of the person elements is not significant, we might prefer to represent the collection as an object with a single key:

{
  "people": {
    "person": [
      {
        "name": "John Doe",
        "address": {
          "street": "123 Main St",
          "city": "Anytown",
          "state": "CA",
          "zip": "12345"
        }
      },
      {
        "name": "Jane Doe",
        "address": {
          "street": "456 Elm St",
          "city": "Othertown",
          "state": "NY",
          "zip": "67890"
        }
      }
    ]
  }
}

We recommend using arrays when the order of elements is significant, and objects when the order is not significant.

Conventions for XML to JSON Mapping

When converting XML to JSON, it's essential to choose a convention that works for your specific use case. We recommend using a combination of the Parker and GData conventions, as they provide a clear and consistent way to handle attributes, namespaces, and collections.

Key Takeaways

  • Use the Parker convention to handle attributes and mixed content.
  • Use the GData convention to handle namespaces and XML schemas.
  • Choose between arrays and objects based on the significance of element order.
  • Use a consistent convention throughout your conversion process.

FAQ

Q: What is the difference between Badgerfish and Parker conventions?

The Badgerfish convention treats attributes as nested objects, while the Parker convention treats attributes as key-value pairs at the same level as the element's content.

Q: How do I handle XML schemas in my conversion process?

Use the GData convention to prefix namespace-qualified elements with the namespace URI.

Q: What is the best way to represent collections of elements in JSON?

Use arrays when the order of elements is significant, and objects when the order is not significant.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp