XML still feeds a large part of the web's plumbing: feeds, exports, configuration formats and older APIs all speak it. JSON is what most modern code wants to work with. Converting between them sounds mechanical, but XML carries things JSON has no native place for, above all attributes and text that sits alongside nested elements. A converter has to make decisions about those, and you need to know what the decisions were before you build on the output.

The free XML to JSON Converter on Ovatly parses your XML with the browser's built-in DOMParser and applies a clear, documented set of conventions. This guide explains those conventions, walks through the conversion, and warns you about the one behaviour that catches almost everyone: the shape of the output depends on how many times each tag appears.

Contents

  • Quick answer
  • The conventions: where attributes and text go
  • The shape warning: single values versus arrays
  • What does not survive the conversion
  • How to convert XML to JSON, step by step
  • Worked example: a person element
  • Limitations and honest tradeoffs
  • Common mistakes
  • FAQs
  • Conclusion
  • References

Quick answer

To convert XML to JSON: paste your XML text, or upload an .xml file, into the XML to JSON Converter and press Convert to JSON. Attributes become keys starting with @, repeated elements become arrays, and text that sits next to child elements or attributes is kept under #text. Review the result, then copy it or download it as converted.json. The parsing happens in your browser; the tool never fetches XML from a URL and uploads nothing.

The conventions: where attributes and text go

Attributes become @keys. An attribute on an element is written as a key starting with @ inside that element's object. An element carrying the attribute id produces the key @id, which keeps attribute values visibly separate from child elements that happen to share a name.

Text beside elements is kept under #text. When an element has both text and child elements, or text and attributes, the text cannot simply become the value, because the value is now an object. It is preserved under the key #text instead. Nothing is silently dropped.

An element with only text becomes that text. If an element contains nothing but text, there is no ambiguity: its value in the JSON is the text itself, a plain string with no wrapper object and no #text key.

These three rules cover most real documents. The fourth behaviour is not a rule about where things go, but about how many of them there are, and it deserves its own section.

The shape warning: single values versus arrays

Repeated sibling elements with the same tag become a JSON array. A single occurrence stays a single value. That sounds reasonable, and it is, but it means the shape of your JSON depends on the document you fed in.

Consider a feed where most entries have one contact element and a few have two. In the converted JSON, the contact field is an object for the first kind of entry and an array for the second. Two documents from the same source, converted on different days, can produce different shapes for the same field. Code that consumes the output has to allow for both, or normalize the field after conversion. Knowing this in advance is the difference between a conversion you can rely on and a bug that appears only when a supplier sends a second phone number.

What does not survive the conversion

Three parts of an XML document are not carried into the JSON. XML comments are dropped. Processing instructions are dropped. Namespaces are not carried over either: the namespace prefix appears as part of the tag name, so a prefixed element keeps its prefix in the resulting key, but the namespace declaration itself, and the distinction between two prefixes bound to the same namespace, is lost.

None of this is hidden. If your document's meaning depends on comments or on namespace identity rather than on the literal prefixed names, JSON is the wrong target format for it, and no converter can change that.

How to convert XML to JSON, step by step

  1. Open the XML to JSON Converter and paste your XML text, or upload an .xml file.
  2. Press Convert to JSON.
  3. Review the JSON, watching for @ attribute keys and #text keys, and check which fields came out as arrays.
  4. Copy the result, or download it as converted.json.

Worked example: a person element

Take this small XML document:

<person id="7"><name>Ana</name></person>

The conversion produces:

{
  "person": {
    "@id": "7",
    "name": "Ana"
  }
}

Read it against the conventions. The element person becomes the key person. Its attribute id becomes the key @id with the value "7", clearly marked as an attribute rather than a child. The child element name holds only text, so it becomes the plain string "Ana". And because name appears exactly once here, it stays a single value; a second name element under the same parent would have turned it into an array.

Limitations and honest tradeoffs

The shape warning bears repeating as a limitation: the converter reflects the document in front of it, so fields are arrays only where tags repeat. It will not force a field into an array for consistency with documents it has never seen.

Malformed XML stops the conversion entirely. Mismatched or unclosed tags and other well-formedness errors are reported in plain words, and no partial JSON is produced from a broken document. That is the right behaviour, but it means a single broken tag in a large export blocks the whole file until you fix the source.

Finally, every value in the output is text at heart. XML does not type its values, so numbers and booleans arrive as strings, and any code consuming the JSON should convert them deliberately rather than assume types.

Common mistakes

  • Assuming a field is always an array. A tag that appears once stays a single value. Write consuming code that handles both shapes, or normalize after converting.
  • Hunting for lost text. Text that sat beside child elements or attributes is under the #text key. It was preserved, just not where a first glance expects it.
  • Confusing attributes with elements. Attributes carry the @ prefix in the output. If a key looks missing, check for its @ form before assuming it was dropped.
  • Expecting namespaces to survive. They do not. Prefixes remain part of tag names, but namespace declarations and identity are not carried over.
  • Pasting broken XML and expecting partial output. A well-formedness error stops everything and is reported in plain words. Fix the source document first.

FAQs

Why is there a #text key in my JSON? It preserves text that sits next to child elements or attributes, which would otherwise have nowhere to go in JSON. An element holding only text becomes that text directly, with no #text key.

Why is one field an array and another not? Only tags that repeat under the same parent become arrays. A tag that appears once stays a single value, so check whether your code needs to handle both shapes.

What counts as invalid XML? Mismatched or unclosed tags and other well-formedness errors. The parser reports them in plain words and no JSON is produced from a broken document.

What happens to XML namespaces? Namespaces are not carried into the JSON. The prefix stays as part of the tag name, so a prefixed element keeps its prefix in the resulting key.

Can the tool fetch XML from a URL? No. Paste the XML or upload the file; nothing is fetched and nothing is uploaded, and the parsing happens in your browser.

Conclusion

XML to JSON conversion is a matter of conventions, and these are simple enough to hold in your head: @ keys for attributes, #text for loose text, arrays only where tags repeat, and plain strings for elements that hold nothing but text. The XML to JSON Converter applies them locally in your browser and returns the file as converted.json. If you later need to go back the other way, our companion guide on how to convert JSON to XML uses the same conventions in reverse. For more guides in this series, see our Data and JSON Tools guides.

References