← Guides

Developer Guide

JSON vs XML

JSON vs XML for APIs: objects versus tags, native parse versus markup trees, and which local tool to use. The XML validator is a tag-stack check, not DOMParser.

json vs xmljson or xml apixml versus jsondata format comparisonrest api json xml

JSON and XML both serialize structured data as text. JSON won default HTTP APIs because it maps to objects, arrays, strings, numbers, booleans, and null with almost no ceremony, and JSON.parse is native in the browser. XML still wins for documents: elements, attributes, namespaces, mixed content, and a published XSD that many vendors already implement.

This is not a loyalty test. It is a mapping problem. The tools on this site stay in the browser and they are not interchangeable either. /tools/json-formatter pretty-prints JSON. /tools/xml-viewer indents XML after a nesting check. /tools/xml-validator is that nesting check. /tools/xml-to-json is the one that actually uses DOMParser.

Syntax: braces versus matching tags

JSON uses { }, [ ], and quoted keys. XML uses matching tags and optional attributes. The same record is usually shorter in JSON because you do not repeat the element name on close. XML can store metadata on an attribute without a nested element, and it can mix text with child elements — something JSON cannot do without a convention (#text, a content array, and so on).

Trailing commas, single quotes, and comments are JavaScript, not JSON. /tools/json-formatter reports the failing line and column. Crossed tags are an XML problem; the formatter will not help you there.

Parsing: JSON.parse versus a markup tree

In the browser, JSON.parse is built in. XML needs a parser that understands tags. /tools/xml-to-json walks the tree with DOMParser (application/xml): the document element becomes the JSON root key, repeated sibling tags become arrays, and attributes land under @attributes when that option is on.

/tools/xml-validator does not use DOMParser. It is a tag-stack check: start tags push, end tags pop, self-closing /> never push. A mismatch prints Expected </item> but found </root>. It does not check a single root, quoted attributes, namespaces, character references, DTDs, or an XSD. Passing that check is not schema validity.

Pretty-print and inspect

/tools/json-formatter formats, minifies, sorts keys, shows a collapsible tree, and downloads .json. Invalid JSON is an error with line and column, not a guessed repair.

/tools/xml-viewer runs the same tag-stack check as the validator, then rewrites whitespace: two-space indent, tags on their own lines, inline text such as <name>Jane</name> kept on one line. If nesting fails, it prints the validator message instead of inventing indentation. It is not a DOM round-trip and will not reorder attributes.

When JSON is the API default

Public REST and GraphQL APIs, mobile and SPA clients, package.json, logs, and anything a JavaScript stack will parse a thousand times a second. Pair the payload with /tools/json-formatter before you argue that the server “dropped a field” — trailing commas are more common than vanished keys.

JSON has no namespaces. You nest objects or prefix keys. That is fine for a user list. It is awkward for a document that must mix two vocabularies.

When XML still wins

SOAP, RSS/Atom, sitemaps, Maven POMs, and any partner who already publishes an XSD. Mixed content and attributes are native. Namespaces let two schemas share a document.

Convert a payload with /tools/xml-to-json for display in a JSON-shaped client. Do not assume a round-trip preserves attributes, comments, or namespace prefixes unless you defined that mapping. Validate nesting on /tools/xml-validator first if DOMParser reports a parsererror.

Common pitfalls

Treating the XML validator as an XSD checker. Treating XML Viewer as DOMParser. Assuming every <item> becomes a JSON array — a single child stays a bare object until a second sibling with the same tag appears.

On the JSON side: returning HTML error pages from /api/*, and using 200 with {"success":false} so HTTP-aware clients never see a failure.

  • /tools/xml-validator is a tag-stack, not DOMParser and not XSD.
  • /tools/xml-to-json is the DOMParser-based converter.
  • Tree view and line-column errors for JSON live on /tools/json-formatter.

Worked examples

The same record in both formats

JSON:
{"id":"u_1","name":"Ada"}

XML:
<user id="u_1"><name>Ada</name></user>

Format the JSON in /tools/json-formatter. Indent the XML in /tools/xml-viewer. Convert the XML with /tools/xml-to-json if you need @attributes.

Tag-stack pass versus a crossed pair

Input
<root><item id="1">Valid XML</item></root>  versus  <a><b></a></b>
Output
XML is valid.  ·  Invalid XML: Expected </b> but found </a>.

Both results come from /tools/xml-validator (and from the viewer’s first pass). No schema was applied. DOMParser is not involved.

Frequently asked questions

Is XML Validator a full XML 1.0 parser?

No. /tools/xml-validator checks that start and end tags nest. It skips comments and processing instructions. It does not use DOMParser and does not load an XSD.

Which tool uses DOMParser?

/tools/xml-to-json. The viewer and validator share a tag-stack instead.

Should a new HTTP API use XML?

Default to JSON unless a document model, a partner XSD, or an existing SOAP/RSS contract requires XML.

Will XML to JSON round-trip cleanly?

Not by default. Attributes, mixed content, and namespaces need a mapping. Convert for inspection; do not treat the JSON as a lossless clone.

Are any of these uploaded?

No. Formatter, viewer, validator, and converter all run in the browser.

Summary

Pick JSON for object-shaped APIs and XML for document-shaped contracts. Pretty-print JSON on /tools/json-formatter, indent XML on /tools/xml-viewer, check nesting on /tools/xml-validator (tag-stack, not DOMParser), and convert with /tools/xml-to-json when you actually need a JSON tree.

Try it yourself

Put what you've learned into practice with our free browser-native tool.

Open Tool →