Plenty of systems still export XML: older ERPs, supplier feeds, government data, device logs. Plenty of modern APIs accept only JSON. So the integration work starts with a translation step, and the translation is where integrations quietly go wrong, because XML and JSON do not describe data in the same way. XML has attributes as well as elements. JSON has neither attributes nor any notion of an element that repeats.

The conversion itself is one paste. The real skill is reading the output correctly, because the JSON shape follows a small set of rules, and one of them will surprise you if nobody warns you. This guide walks through converting with the free XML to JSON Converter on Ovatly, then explains the shape rules and the one caveat that matters most for API work.

Contents

  • Quick answer
  • The shape rules: how XML becomes JSON
  • How to convert an XML file to JSON, step by step
  • Worked example: a person record with an attribute
  • The single-occurrence trap
  • Limitations and honest tradeoffs
  • Common mistakes
  • FAQs
  • Conclusion
  • References

Quick answer

To convert an XML file to JSON for a modern API: paste the XML into the XML to JSON Converter, which parses it in your browser and returns JSON. Read the result with four rules: attributes become @ prefixed keys, repeated elements become arrays, text sitting beside attributes or child elements lands under a #text key, and an element holding only text becomes the text itself. Then design for the caveat: an element that appears once stays single, so any element that can repeat may arrive as either one value or an array, and your API consumer has to accept both.

The shape rules: how XML becomes JSON

The converter uses the browser's built-in DOMParser and maps the document with consistent rules.

Attributes become @ keys. An attribute cannot be a plain key, because it would collide with a child element of the same name. So an id attribute appears as @id inside the element's object, clearly marked as having come from an attribute.

Repeated elements become arrays. When the same element appears two or more times under one parent, the JSON value is an array with one entry per occurrence, in document order.

Text has two homes. If an element contains only text, the element simply becomes that text: a string value. If the element also has attributes or child elements, the text moves to a #text key inside the object, so it is not lost and does not overwrite anything.

Single occurrences stay single. This is the rule the next-but-one section is devoted to. An element that appears once is a single object or string, never a one-item array.

Two further behaviours are worth knowing before you rely on the output. Comments, processing instructions and namespaces are not carried over, so the JSON contains data only. And if the XML is not well-formed, the converter reports the problem in plain words rather than guessing at a repair.

How to convert an XML file to JSON, step by step

  1. Get the XML into text form. Open the file in any text editor, or copy the response body from wherever the legacy system delivered it. The XML to JSON Converter never fetches a URL, so the paste is the input.
  2. Paste the XML into the converter and run it. The parsing happens in your browser with DOMParser; nothing is uploaded.
  3. Read the output against the shape rules above before you copy it anywhere. Find the @ keys, check which elements became arrays, and note any #text keys.
  4. Copy the JSON into your API payload or save it, and then handle the single-occurrence trap described below in the code that will consume files like this one.

Worked example: a person record with an attribute

Here is a small XML record with one attribute and a repeated element:

<person id="7">
  <name>Ana</name>
  <tag>a</tag>
  <tag>b</tag>
</person>

The converted JSON is:

{
  "person": {
    "@id": "7",
    "name": "Ana",
    "tag": ["a", "b"]
  }
}

Read it with the rules. The id attribute became @id. The <name> element held only text, so it became the string "Ana" directly. The <tag> element appeared twice, so it became an array of two strings. Every part of the original document is accounted for, and each part's origin is still visible in the shape it took.

The single-occurrence trap

Now remove one <tag> from that record, so only <tag>a</tag> remains. The output changes in a way that breaks naive code:

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

The tag value is now a plain string, not an array holding one string. This is not a bug and not inconsistency: the shape follows the document, and a single occurrence stays single. But it means a feed that sends two tags today and one tag tomorrow produces two different JSON shapes for the same field. Any API consumer, importer or script reading this JSON must handle repeatable elements as either a single value or an array, typically by wrapping a lone value in an array before looping over it. Decide that handling once, at the boundary where the JSON enters your system, and every later file is safe regardless of which shape it arrives in.

Limitations and honest tradeoffs

The conversion is deliberately lossy about XML's extras. Comments, processing instructions and namespaces are dropped, so a document whose meaning depends on namespace prefixes loses that layer. Attribute order and element order among different names carry no significance in the output beyond what the mapping preserves. Malformed XML is reported in plain words, which is honest, but the converter will not repair it for you: fixing the source document is your job.

There is also no fetching and no upload. The tool never retrieves a URL, and everything runs in your browser, which keeps private data on your device but means large-scale or scheduled conversions belong in your own pipeline, using files converted here as the shape reference.

Common mistakes

  • Assuming repeatable elements are always arrays. They are arrays only when they repeat in that document. One occurrence is a single value, and code that always expects an array will fail on exactly the smallest, simplest files.
  • Looking for attributes under plain keys. Attributes live under @ prefixed keys. A consumer searching for id where the JSON holds @id finds nothing.
  • Expecting comments or namespaces to survive. They do not. If the API needs that information, it has to be supplied another way.
  • Pasting a URL and waiting. The converter never fetches. Paste the document text itself.
  • Trusting the shape from one sample file. Test with a file where repeatable elements appear once, and one where they appear several times, before fixing your consumer's expectations.

FAQs

Where do XML attributes go in the JSON? Attributes become keys prefixed with @ inside the element's object. An id attribute on a person element appears as @id alongside the keys for its child elements, so attributes and elements never collide.

Why is my repeated element sometimes an object and sometimes an array? Because the shape follows the document. An element that appears two or more times becomes an array; an element that appears once stays a single object or string. The same feed can produce both shapes on different days, so code that consumes the JSON must handle each repeatable element as either form.

What does the #text key mean? It holds an element's text when the element also carries attributes or child elements. If an element contains only text and nothing else, it becomes the text itself and no #text key appears.

What happens to XML comments and namespaces? They are not carried over. Comments, processing instructions and namespace information are dropped during conversion, so if your API depends on them, that information has to travel by another route.

Does the tool fetch the XML from a URL for me? No. The converter never fetches a URL. You paste the XML into the page, and the conversion happens in your browser using its built-in DOMParser.

Conclusion

Converting XML to JSON is a paste; integrating it safely is a reading skill. Learn the four shape rules, design your consumer for the single-occurrence trap, and files from any legacy system become predictable. The XML to JSON Converter keeps the mapping consistent, so the shape you plan for is the shape you get. If your data flows the other way and you are about to hand converted XML to another system, our pre-send checklist in how to check converted XML before sending it on is the companion read. For more guides in this series, see our Data and JSON Tools guides.

References