Guides

XML to JSON: Preserving Attributes, CDATA & RSS Feeds

Convert XML, RSS, and SVG files to JSON in browser RAM while preserving @_ attributes, CDATA text, numeric types, and round-trip XML structure.

4 min read

Quick Answer: To convert XML, RSS, or SVG documents into JSON without dropping metadata, keep Ignore Attributes turned off and use the @_ attribute prefix. TxtTo validates XML line and column errors locally, unwraps CDATA blocks into clean JSON strings, coerces numbers and booleans when Parse Tag Value is enabled, and round-trips @_ attributes back into valid XML.

Why XML attributes, text nodes, and CDATA clash with JSON

Unlike JSON objects that store flat key-value pairs, XML elements mix tag attributes, inner character data, repeated sibling nodes, and CDATA blocks that hold unescaped HTML or code.

Naive converters either throw away XML attributes completely or flatten repeated RSS item tags inconsistently, breaking downstream TypeScript types and API parsers.

  • Element attributes like id="bk101" live inside opening tags rather than child keys.
  • Repeated sibling elements such as RSS item entries must map cleanly into JSON arrays.
  • CDATA sections embed markup or scripts containing < and & without entity escaping.
xml
<catalog currency="USD">
  <book id="bk101">
    <title><![CDATA[Browser <em>XML</em> Guide]]></title>
    <price>39.00</price>
  </book>
</catalog>

Controlling ignoreAttributes, attributePrefix (@_), and parseTagValue

TxtTo runs fast-xml-parser entirely inside browser RAM so you can tune attribute and type behavior live without sending proprietary feeds or SOAP payloads over the network.

Leave ignoreAttributes disabled and keep attributePrefix at @_ so attribute values stay distinguishable from child tags. Disable parseTagValue whenever leading zeros in postal codes, invoice numbers, or SKU identifiers must stay strings instead of numbers.

json
{
  "catalog": {
    "@_currency": "USD",
    "book": {
      "@_id": "bk101",
      "title": "Browser <em>XML</em> Guide",
      "price": 39
    }
  }
}

Convert XML, RSS & SVG to JSON locally

Validating malformed XML and inspecting RSS or SVG trees

Before parsing, XMLValidator checks well-formedness locally and pinpoints unclosed tags or mismatched attributes by exact line and column number. That catches broken RSS exports or truncated SVG markup before data enters your pipeline.

Once converted, open the output in JSON Formatter to inspect deep hierarchies in Tree View, sort keys alphabetically, or repair trailing commas after manual edits.

Inspect & format converted JSON trees

OptionDefaultBehaviorWhen to Change
ignoreAttributesfalseKeeps all XML tag attributes in JSONEnable only if attributes are noisy layout hints
attributePrefix@_Prepends @_ to attribute keysChange if downstream consumer expects _ or $
parseTagValuetrueCoerces numeric and boolean stringsDisable to preserve leading zeros like 00412
indent2Formats JSON with 2-space indentationSet to 0 for compact single-line output

Round-tripping JSON back to XML with rootName & includeDeclaration

Because valid XML requires a single outer root element, JSON to XML wraps top-level arrays under <item> tags inside your chosen rootName (defaulting to root) and prepends <?xml version="1.0" encoding="UTF-8"?> when includeDeclaration is active.

Any JSON key carrying the @_ prefix serializes straight back into an XML attribute, and you can compare edited round-trips locally with Diff Checker.

Serialize JSON back to formatted XML

References

Frequently asked questions

How are XML CDATA blocks handled when converting to JSON?

CDATA wrappers are unwrapped into clean string values in JSON without double-escaping HTML tags or ampersands.

How do I preserve leading zeros like ZIP codes or SKU IDs in XML to JSON?

Turn off Parse Tag Value so numeric-looking text such as 02139 remains a JSON string rather than converting to 2139.

Can JSON to XML recreate XML attributes from converted JSON?

Yes. Any property starting with @_ in your JSON object is automatically written back as an attribute on its parent XML element.

Related formats

Related Comparisons

Related Guides