{}JSONLint.app

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

Common Mistakes & Pro Tips

  • Attributes and text need a naming conventionXML has both attributes and child text, but JSON objects have only keys, so converters commonly prefix attributes (e.g. "@id") and store element text under "#text". So <user id="5">Ann</user> becomes {"user": {"@id": "5", "#text": "Ann"}} rather than losing the attribute.
  • Repeated elements become an array only when there are two or moreA single <item> maps to an object, but two or more sibling <item> elements map to an array. This means the same schema produces a one-element list in some files and a bare object in others, so code defensively or use a setting that always forces arrays for known repeating tags.
  • Everything is a string — XML has no native typesNumbers, booleans, and dates all arrive as text: <age>30</age> becomes "30", not 30. Add type coercion only deliberately, since XML content like "007" or "1.10" may be meaningful as a string.
  • Namespaces and prefixes leak into keysPrefixed tags like <ns:title> usually become a key such as "ns:title", and xmlns declarations may appear as "@xmlns" attributes. If you don't care about namespaces, strip the prefixes during conversion to get cleaner keys.
  • Mixed content and whitespace can surprise youElements containing both text and child elements (mixed content) don't map cleanly to JSON, and insignificant whitespace/newlines between tags can show up as stray "#text" entries. Trimming whitespace text nodes avoids polluting the output with empty strings.

Frequently Asked Questions

How are XML attributes represented in the JSON output?

Attributes are kept but distinguished from child elements, typically by an "@" prefix on the key, so <book isbn="123"> yields {"book": {"@isbn": "123"}}. Element text content sits alongside under a key like "#text". This convention is necessary because JSON has no separate concept of attributes.

Why is one of my lists an object instead of an array?

Converters can't know a tag is meant to repeat when only one instance is present, so a single child becomes an object and multiple identical siblings become an array. Either handle both shapes in your code or enable a "force array" option for the elements you know are collections.

How do I convert XML with namespaces?

By default, namespace prefixes are preserved in the keys (for example "soap:Body"), and xmlns attributes are carried through. If the prefixes add noise, turn on namespace stripping to drop them, but only do this when you're sure two different namespaces won't collide into the same key.

Does the converter validate the XML or just parse it?

It parses well-formedness — tags must be balanced and properly nested — and will report an error on malformed input. It does not validate against a DTD or XSD schema, so structurally valid XML that violates a schema will still convert.

How do I get numbers and booleans instead of strings?

Enable type inference/coercion, which converts text like "42" to 42 and "true" to a boolean. Leave it off when element values are codes or identifiers (such as "0700") that must keep their exact text, since coercion would alter them.

Is my XML sent to a server?

No. Parsing uses the browser's built-in XML capabilities and runs locally, so the document never leaves your computer. This is important for XML payloads like SOAP messages or invoices that may contain private data.