XML to Compressed JSON

Parses XML elements, attributes, and text content into a plain JSON structure using the same @-prefixed attribute and repeated-tag-as-array mapping as the standard XML to JSON converter, then serializes the result as compact, whitespace-free JSON instead of pretty-printing it. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-26

Overview

Introduction

Sometimes the destination for converted XML data isn't a code editor but a URL parameter, a log line, or a network payload, places where every extra whitespace byte adds up. This converter turns XML straight into JSON that's already minified, so there's no separate minify step after the conversion.

It runs entirely in your browser: paste XML in, get compact JSON out, with the same parsing this site's other XML tools use.

What Is XML to Compressed JSON?

An XML-to-compressed-JSON converter walks a parsed XML document tree and maps its elements, attributes, and text content onto an equivalent plain JavaScript value, then serializes that value as JSON with no indentation, no line breaks, and no extra spaces.

It uses a hand-written parser rather than the browser's DOMParser, so conversion behaves identically whether it runs client-side or during any future server-side rendering pass.

How XML to Compressed JSON Works

Your XML is tokenized into tags and text runs, then assembled into a tree using a stack-based parser that tracks open and close tags in the same spirit as a SAX parser.

Each element's attributes become @-prefixed keys, an element whose only content is text takes that text as its value directly, and any tag name that repeats among siblings is collected into a JSON array. The resulting value is then passed straight to JSON.stringify with no space argument, which is what keeps the output compact.

When To Use XML to Compressed JSON

Use it when the converted JSON needs to travel somewhere size-sensitive: a query string, a cookie, a QR code, or a compact log entry.

It's also useful any time you want the smallest possible JSON representation of an XML payload without running the output through a separate minifier afterward.

Features

Advantages

  • Runs entirely client-side, so XML containing real config or credentials never leaves your browser.
  • Skips the extra minify step: output is already compact, no need to pipe it through a separate JSON minifier tool.
  • Uses the same attribute/array mapping as the standard XML to JSON converter, so the data shape stays predictable and familiar.
  • Smaller output size makes it easier to paste into space-constrained places like URLs or single-line log entries.

Limitations

  • XML namespaces are folded into the tag name rather than represented as a separate concept.
  • Mixed content (text interleaved with child elements at the same level) is a lossy case for any XML-to-JSON mapping, this converter included.
  • The compact output has no indentation option; use the regular XML to JSON converter if you need pretty-printed JSON for reading or debugging.

Examples

Converting an element with an attribute

Input

<user id="1"><name>Ada</name></user>

Output

{"user":{"@id":"1","name":"Ada"}}

The id attribute becomes @id, and the child <name> element becomes a nested key, all on one line.

Repeated tags become an array

Input

<roles><role>admin</role><role>editor</role></roles>

Output

{"roles":{"role":["admin","editor"]}}

Both <role> siblings share a tag name, so they're collected into a JSON array instead of one overwriting the other.

An unclosed tag is reported, not guessed at

Invalid input

<root><item>value</root>

Parser error

Expected closing tag </item> but found </root>

Conversion requires well-formed XML first; the parser reports exactly where it stopped.

Corrected version

<root><item>value</item></root>

Best Practices & Notes

Best Practices

  • Check how repeated tags convert for your specific document; arrays only form when a tag name repeats among direct siblings, not globally.
  • Validate the resulting JSON's shape before wiring it into code that expects a specific structure, minified output is harder to eyeball than indented output.
  • Run the XML Validator first if you're unsure whether the source document is well-formed.

Developer Notes

This tool calls the same `parseXml` mapping (from `yaml/lib/xml-core.ts`) that powers the XML to JSON Converter, so the data shape is identical between the two pages; the only difference is that this one calls `JSON.stringify(value)` with no space argument instead of `JSON.stringify(value, null, indent)`. Keeping this as a distinct slug/page lets it be discovered directly rather than requiring users to convert then separately minify.

XML to Compressed JSON Use Cases

  • Embedding a parsed XML payload into a URL query parameter or short-lived token where size matters
  • Producing a compact JSON log line from an XML event or request body
  • Minimizing the size of a JSON fixture generated from an XML sample for tests
  • Feeding converted XML data into a network request where bandwidth or payload limits apply

Common Mistakes

  • Expecting XML namespaces to be preserved distinctly; they're currently folded into the tag name.
  • Assuming mixed content (text and elements interleaved) converts cleanly; it's a lossy case for any XML-to-JSON mapping.
  • Forgetting that a single, non-repeated child element becomes an object, not a one-item array, same as the pretty-printed converter.

Tips

  • If you need to read the structure before compressing it, check the regular XML to JSON Converter first, then come back here for the compact version.
  • Pair with the JSON to XML Converter to check what a round trip does to your specific document.
  • Upload a .xml file directly instead of copy-pasting if the document is large.

References

Frequently Asked Questions