For XML vs JSON for APIs, JSON is the better default for a new REST, web or mobile API in 2026: it's lighter, maps straight onto objects in most languages, and is what the tooling expects. XML is still the right choice when you have to speak SOAP, need XSD validation with namespaces, or are joining a system that already exchanges XML.
The short verdict on XML vs JSON for APIs
JSON wins by default for new public APIs, and XML wins when the contract or the partner requires it. The JSON spec, RFC 8259, calls JSON "a lightweight, text-based, language-independent data interchange format". Most API payloads are records, and JSON describes a record with very little ceremony.
XML was built for documents with mixed content, attributes and namespaces. Invoices, publishing formats and enterprise messaging need those features. A simple GET /users/u_ada response just carries the extra weight.
Start with JSON unless someone hands you an XML contract. Picking XML for a greenfield REST API in 2026 buys you more parser setup, more security settings and fewer client libraries, and gets you nothing back.
How the two formats differ
The differences that change the decision are the data model, schemas, encoding rules and security. Here is the same user record in both formats:
{ "id": "u_ada", "name": "Ada Lovelace", "roles": ["admin", "editor"] }
<user id="u_ada">
<name>Ada Lovelace</name>
<roles><role>admin</role><role>editor</role></roles>
</user>
JSON has arrays and objects built in. The XML version has to choose between an attribute and a child element for id, and it needs a wrapper element for the list.
| JSON | XML | |
|---|---|---|
| Data model | Objects, arrays, strings, numbers, booleans, null | Elements, attributes, text, namespaces |
| Schema language | JSON Schema | XSD |
| Encoding over the wire | UTF-8 required between open systems | Declared in the XML declaration |
| SOAP | Not used | Required |
| Main parser risk | Number precision, duplicate keys | XML external entities (XXE) |
Schemas and contracts: XSD vs JSON Schema
Both formats have a mature schema language, so "XML has schemas and JSON doesn't" is out of date. XSD 1.1 "offers facilities for describing the structure and constraining the contents of XML documents, including those which exploit the XML Namespace facility." JSON Schema is "a declarative language for defining structure and constraints for JSON data."
The practical difference is reach. The OpenAPI Specification 3.1.0 uses JSON Schema to describe bodies, and says an OpenAPI document "is itself a JSON object, which may be represented either in JSON or YAML format." Most API gateways, mock servers and SDK generators start from OpenAPI.
XSD still has the edge where namespaces matter, for example when one message combines elements from several standards.
To check JSON against a schema, see the post on validating JSON against JSON Schema.
Where XML is still the right choice
XML is the right choice when the protocol or the other side already uses it. SOAP is the clearest case: the SOAP 1.2 Recommendation says "A SOAP message is specified as an XML Information Set." A SOAP service can't switch to JSON without becoming a different API.
I'd also keep XML when:
- a partner, bank or government system publishes an XSD and expects messages that validate against it;
- the payload is really a document, with markup inside the text;
- one message mixes vocabularies and needs namespaces to keep them apart.
Or serve both. OpenAPI keys each response by media type, and its own example lists application/json and application/xml side by side. The client asks for one with the Accept header.
Security: XXE is an XML-only problem
XML parsers have an attack surface that JSON parsers don't: external entities. The OWASP Top Ten 2017 entry on XXE says "many older XML processors allow specification of an external entity, a URI that is dereferenced and evaluated during XML processing." Its advice includes "use less complex data formats such as JSON" where possible.
If you accept XML, follow the OWASP XXE Prevention Cheat Sheet: "Disable document type definitions (DTDs) whenever possible." Defaults vary by parser. The cheat sheet notes that libxml2 has disabled XXE by default since version 2.9, but it also warns that "Preventing XXE does not prevent every XML denial-of-service attack."
JSON is not risk-free. RFC 8259 only says object names "SHOULD be unique", so two parsers can disagree about a payload with duplicate keys. Numbers are the other trap: the RFC advises expecting no more precision than IEEE 754 binary64 doubles, which is why large IDs get rounded. The post on large numbers in JSON.parse shows that failure and the fix.
Encoding and wire details
JSON's encoding rule is short. RFC 8259 section 8.1 says JSON exchanged between systems outside a closed ecosystem "MUST be encoded using UTF-8", and implementations "MUST NOT add a byte order mark" to networked JSON.
XML lets each document declare its own encoding, which gives you more ways to break a feed. A mismatch between the declared encoding and the real bytes is a classic cause of the XML "not well-formed" parsing error.
What breaks when you move an API from XML to JSON
Moving an existing API from XML to JSON is harder than the format choice suggests, because XML has shapes JSON lacks. Three of them bite:
- Attributes.
<user id="u_ada">has no direct JSON equivalent. Pick"id"or a prefixed key such as"@id", then stick with it. - Repeated elements. A naive converter turns one
<role>into a string and two into an array. Clients crash on the first single-item response. - Mixed content. Text with inline tags, like
<p>Hi <b>there</b></p>, doesn't map cleanly to objects at all.
The posts on converting XML to JSON and mapping XML attributes to JSON cover the conventions in detail.
To see how a given XML payload maps, paste it into the XML to JSON converter. It uses the browser's DOMParser, and the XML stays in the page. It has one real limit: like any converter, it picks a single mapping convention. Treat its output as a draft of your JSON contract, never the contract itself.
FAQ
Is JSON faster than XML?
JSON is usually smaller and simpler to parse because it has no end tags, attributes or namespaces. Real speed depends on your parser and payload, so measure your own case before quoting a figure.
Do SOAP APIs use JSON?
No. SOAP 1.2 defines a SOAP message as an XML Information Set, so a SOAP API sends and receives XML.
Can a REST API return both XML and JSON?
Yes. OpenAPI lets each response list content by media type, such as application/json and application/xml, and the client picks one with the Accept header.
Is XML less secure than JSON?
XML parsers carry an extra risk, XML external entities (XXE). OWASP recommends disabling DTDs and, where possible, using simpler formats such as JSON.
Does JSON have a schema like XSD?
Yes. JSON Schema is a declarative language for defining the structure and constraints of JSON data, and OpenAPI uses it to describe request and response bodies.