Paste your XML below to convert it to JSON:

Loading...
Loading...

When to Convert XML to JSON

XML and JSON both represent structured data, but JSON has become the preferred format for web APIs and modern applications. Convert XML to JSON when:

  • Integrating with REST APIs — Most modern APIs expect JSON
  • Working with JavaScript — JSON parses natively; XML requires a DOM parser
  • Reducing payload size — JSON is typically 30-50% smaller than equivalent XML
  • Migrating legacy systems — Convert old XML configs to JSON

How XML Maps to JSON

The converter handles XML structures like this:

Elements become properties

XML:

<person>
  <name>Alice</name>
  <age>30</age>
</person>

JSON:

{
  "person": {
    "name": "Alice",
    "age": "30"
  }
}

Attributes use @ prefix

XML:

<book id="123" lang="en">
  <title>JSON Guide</title>
</book>

JSON:

{
  "book": {
    "@_id": "123",
    "@_lang": "en",
    "title": "JSON Guide"
  }
}

Repeated elements become arrays

XML:

<items>
  <item>Apple</item>
  <item>Banana</item>
</items>

JSON:

{
  "items": {
    "item": ["Apple", "Banana"]
  }
}

Conversion Options

Our converter uses sensible defaults, but understanding the rules helps:

  • Attributes — Prefixed with @_ to distinguish from child elements
  • Text content — Stored as #text when an element has both text and children
  • Numbers — Kept as strings (JSON doesn't distinguish; convert in your code if needed)
  • Empty elements — Converted to empty string ""
  • CDATA — Extracted as plain text

Common Pitfalls

XML has features JSON doesn't support

  • Namespaces — Preserved but may need manual cleanup
  • Comments — Discarded (JSON doesn't support comments)
  • Processing instructions — Ignored
  • Attribute order — Not guaranteed in JSON objects

Single vs. multiple elements

If your XML sometimes has one <item> and sometimes multiple, the JSON structure changes (single object vs. array). Handle this in your code:

// Ensure items is always an array
const items = Array.isArray(data.items.item) 
  ? data.items.item 
  : [data.items.item];

Pro Tips

  • 💡 Validate both sides — Use the JSON Validator to check your output is valid JSON
  • 💡 Check for encoding issues — Ensure your XML is UTF-8 encoded
  • 💡 Test with edge cases — Empty elements, special characters, deeply nested structures

Programmatic Conversion

Need to convert in code? Here are popular libraries:

  • JavaScript: fast-xml-parser, xml2js
  • Python: xmltodict
  • Java: Jackson XML module
  • Go: encoding/xml + custom mapping

Related Tools