ModernCalcs

XML to JSON

{
  "library": {
    "book": [
      {
        "@id": "1",
        "@genre": "fiction",
        "title": "The Great Gatsby",
        "author": "F. Scott Fitzgerald",
        "year": "1925",
        "price": "12.99"
      },
      {
        "@id": "2",
        "@genre": "non-fiction",
        "title": "Thinking, Fast and Slow",
        "author": "Daniel Kahneman",
        "year": "2011",
        "price": "15.99"
      }
    ]
  }
}

Attributes become @attribute keys. Repeated sibling elements become arrays. Text content becomes a string value or #text when mixed with attributes/children.

XML to JSON: Convert Legacy XML Feeds and Configs to JSON

XML was the universal data format before JSON took over. RSS feeds, SOAP APIs, configuration files, and many legacy systems still use XML. This converter parses XML and produces idiomatic JSON — attributes prefixed with @, repeated sibling elements merged into arrays, text-only elements simplified to string values.

Formula
<library> <book id="1"><title>Gatsby</title></book> <book id="2"><title>Moby Dick</title></book> </library> → JSON: { "library": { "book": [ { "@id": "1", "title": "Gatsby" }, { "@id": "2", "title": "Moby Dick" } ] } }

Attributes → @attr keys. Two or more same-name siblings → array. Single element → object (not array). Text-only element → string value.

XML-to-JSON Conventions

There is no single standard for XML-to-JSON mapping. The most common convention (used by xml2js, xml-js, and this tool) is: attributes get a @ prefix; text content gets a #text key when mixed with other properties; repeated sibling elements become arrays; single elements stay as objects. Some tools always produce arrays (even for single elements) — if your consumer expects that behavior, you'll need to adjust the output.

Use Cases

Converting RSS/Atom feeds for processing in JavaScript. Parsing SOAP API responses to extract data. Reading Android resource XML files. Consuming configuration XML from legacy systems. Transforming Maven pom.xml for dependency analysis. The output is valid JSON that can be queried with JSONPath or processed with jq.

After Conversion: Querying with JSONPath

Once you have JSON, use the JSONPath Finder tool to query specific values. $..title extracts all title values recursively. $.library.book[*].@id extracts all book IDs. $.library.book[?(@.@genre == 'fiction')] filters by attribute value. This is far more expressive than XPath and works with standard JSON tools.

Conversion Rules

  • Attributes → @attribute keys
  • Repeated siblings with same tag → JSON array
  • Single element → JSON object
  • Text-only elements → string value
  • Mixed content (text + children) → #text key

Frequently Asked Questions

How are XML attributes converted?

XML element attributes are converted to JSON keys with an @ prefix. For example, becomes { "@id": "1", "@genre": "fiction", ... }. This convention distinguishes attributes from child element keys and is the most common XML-to-JSON mapping used by tools like xml2js.

How are repeated elements handled?

When multiple sibling elements have the same tag name, they are collected into a JSON array. For example, three elements under become library: { book: [{...}, {...}, {...}] }. A single element stays as an object, not an array. This matches the most common xml2js/xml-js behavior.

What happens to element text content?

If an element has only text content (no attributes or child elements), the text becomes the value directly. For example, Gatsby becomes { "title": "Gatsby" }. If the element also has attributes or children, the text is stored under a '#text' key.

What XML features are not supported?

Namespaces (xmlns, ns:element) are preserved as-is in tag names. XML entities (& < > " ') are left un-decoded by this parser — pass clean XML with no custom entities. CDATA sections are converted to their text content. Processing instructions () are stripped. DTD declarations are stripped.