JSON has no attributes. That single fact explains every surprise people meet when they convert XML to JSON, because XML elements carry two kinds of information: child elements and attributes. JSON objects offer one kind of slot, the key, so the converter has to fit both kinds into it without letting them collide.

The XML to JSON Converter on Ovatly solves this with two markers: an @ prefix for attributes and a #text key for element text that has company. This guide explains both rules, shows one element in three forms so you can see exactly what changes, and covers the mistakes that follow when code expects attributes to behave like children.

Contents

  • Quick answer
  • Why attributes are the awkward part
  • The @ prefix rule
  • Where the text goes: the #text key
  • Worked example: one element, three forms
  • How to see the rules on your own file, step by step
  • Limitations and honest tradeoffs
  • Common mistakes
  • FAQs
  • Conclusion
  • References

Quick answer

An attribute becomes a key prefixed with @ in the JSON object for its element, sitting beside any child elements. If the element also holds text, the text moves to a #text key. If the element holds text only, with no attributes, there is no object at all: the element becomes the text itself. Attribute values always stay strings. Paste an element into the XML to JSON Converter and you can watch each rule apply to your own data.

Why attributes are the awkward part

In XML, an element's attributes and its children are different things. Attributes describe the element itself: an id, a language, a type. Children are content the element contains. The two can even share a name without conflict, because they live in different places in the document.

JSON gives you one mechanism: keys in an object. If attributes and children were both copied across as plain keys, an attribute and a child with the same name would overwrite each other, and you could not tell from the JSON which was which. The @ prefix is the fix. It marks a key as an attribute, keeps it distinct from any child of the same name, and does it without dropping information.

The @ prefix rule

Every attribute on an element becomes a key that starts with @. An attribute written as lang with the value en becomes the key @lang in the JSON object for that element. The @ keys sit in the same object as the child elements, so one object holds the element's attributes alongside its content.

Attribute values stay strings, exactly as written. XML attribute values are text in the source, and the converter does not guess at types: an attribute holding 3 arrives as the string 3, and an attribute holding true arrives as the string true, not a boolean.

Where the text goes: the #text key

An element that holds only text becomes the text: a <name> element containing Ana converts to a name key with the value Ana, with no object involved. That simple case is why the attribute case surprises people.

Add one attribute and the element needs an object to hold the @ key, so the text can no longer be the value itself. It moves to a #text key inside the same object. The same applies to text that sits beside child elements: the text goes under #text, next to the keys for the children. Nothing is dropped; the text just gets a named place to live.

Worked example: one element, three forms

Start with text only:

<name>Ana</name>
{ "name": "Ana" }

The element holds text and nothing else, so the value is the text itself. Now add an attribute:

<name lang="en">Ana</name>
{
  "name": {
    "@lang": "en",
    "#text": "Ana"
  }
}

The attribute becomes @lang, and the text moves to #text. Finally, give the element children instead of text:

<name lang="en"><first>Ana</first><last>Novak</last></name>
{
  "name": {
    "@lang": "en",
    "first": "Ana",
    "last": "Novak"
  }
}

With children, the @ key sits beside the child keys, and there is no #text, because the element holds no text of its own. The two child elements hold text only, so each became a plain string.

How to see the rules on your own file, step by step

  1. Open the XML to JSON Converter and paste a small piece of your XML, ideally one element with attributes.
  2. Convert it and find the @ keys in the result. Each one matches an attribute in your source.
  3. Check whether the element held text. If it did, look for the text under #text; if the result is a plain string, the element had no attributes.
  4. If the XML is not well formed, the tool reports the problem in plain words. Fix the source and convert again rather than editing the JSON by hand.

Limitations and honest tradeoffs

Namespaces are not carried over. An attribute that belongs to a namespace loses that context in the JSON, and namespace declarations do not appear at all.

Comments and processing instructions are not carried over either, including comments written next to the attributes they explain.

The @ prefix and #text key are this converter's mapping. JSON itself defines neither, so code written for the output of a different converter may expect different markers.

Repeated child elements become arrays, but attributes never do: each element carries its own set, so an attribute on a repeated element appears once inside each object in the array.

Common mistakes

  • Looking for an attribute under its plain name. It is stored under the @ name. A lookup for lang fails; @lang succeeds.
  • Expecting the text as the element's value. Once an attribute exists, the value is an object and the text is under #text.
  • Treating attribute values as typed data. Values holding 3 or true are strings. Convert them where your code reads them.
  • Assuming one fixed shape for an element. The same element name converts to a string in one document and an object in another, depending on whether attributes are present.

FAQs

Why do converted attributes start with @? JSON has no separate place for attributes, so the converter marks them with an @ prefix to keep them distinct from child elements. An attribute called id becomes the key @id, while a child element called id would stay id. The prefix is how you tell the two apart in the result.

What is the #text key? It holds the text of an element that also carries attributes or sits beside child elements. Text on its own becomes the value directly, but once an element has an attribute, the text needs its own key, and that key is #text.

Do attribute values become numbers or booleans? No. Attribute values stay strings exactly as written. An attribute holding 3 arrives as the text 3, not the number 3, and code that needs a number converts it after reading.

What happens to namespaces on attributes? Namespaces are not carried over by the converter, so namespace declarations do not appear in the JSON and a namespaced attribute loses its namespace context. If your document leans on namespaces, check the converted result carefully before relying on it.

Does the converter fetch the XML from a URL? No. It never fetches a URL. You paste the XML into the XML to JSON Converter, and the DOMParser in your browser does the conversion on your device.

Conclusion

Attributes survive conversion, but they change their address: they become @ keys inside the element's object, and any text they share the element with moves to #text. Keep those two rules in mind and the output of the XML to JSON Converter reads exactly as it was written. If you are converting a complete config file rather than inspecting one element, How to Convert XML Config Data into JSON Settings covers the whole workflow. More tools and guides are collected in our Data and JSON Tools guides.

References