JSON vs XML vs YAML: Differences With Examples

JSON vs XML vs YAML: Differences With Examples

JSON, XML and YAML all store structured data as text. JSON is the standard for web APIs, YAML is popular for config files because it is easy to read, and XML is still used for documents, feeds and older enterprise systems. The easiest way to see the difference is to look at the same data in all three.

Working with JSON right now? Format and validate it in the free JSON Formatter.

Open the JSON Formatter

The same data, three ways

JSON

{
  "user": {
    "name": "Ayesha Khan",
    "age": 28,
    "active": true,
    "skills": ["PHP", "Vue.js", "MySQL"]
  }
}

XML

<user>
  <name>Ayesha Khan</name>
  <age>28</age>
  <active>true</active>
  <skills>
    <skill>PHP</skill>
    <skill>Vue.js</skill>
    <skill>MySQL</skill>
  </skills>
</user>

YAML

user:
  name: Ayesha Khan
  age: 28
  active: true
  skills:
    - PHP
    - Vue.js
    - MySQL

Key differences

JSONXMLYAML
ReadabilityGoodWordyVery easy
File sizeSmallLargest (closing tags)Smallest
Data typesString, number, boolean, null, array, objectEverything is text unless a schema says otherwiseSame as JSON, plus dates and more
CommentsNot allowedAllowedAllowed (#)
Whitespace mattersNoNoYes, indentation is structure
Native in browsersYes (JSON.parse)Yes (DOMParser)No, needs a library
Typical useREST APIs, web apps, NoSQLSOAP, RSS, sitemaps, Office files, SVGDocker Compose, Kubernetes, CI pipelines

When to use JSON

Use JSON for anything sent between a browser and a server. Every language has a fast built-in parser, and JavaScript reads it natively. Frameworks make it a one-liner: Laravel turns arrays and models into JSON automatically, Flask does the same for dictionaries, and Django has JsonResponse. To find your way around a real response, see How to Read a JSON API Response.

When to use XML

Use XML when a system requires it: SOAP web services, RSS feeds, sitemaps, invoices in some government and banking systems, and document formats. XML also supports attributes, namespaces and strict schema validation (XSD), which some industries rely on.

When to use YAML

Use YAML for files that people edit by hand, such as docker-compose.yml, GitHub Actions workflows and Kubernetes manifests. Comments and the clean layout make it easy to maintain. Be careful with indentation: one wrong space changes the meaning or breaks the file. Sharing one for help? Follow the config file redaction checklist first, because YAML files often hold passwords and keys.

A note on YAML surprises

YAML guesses data types. In older YAML 1.1 parsers (still common, such as PyYAML), values like no, yes, on and off become booleans, so a country code NO (Norway) can turn into false. Quote strings that could be misread: country: "NO". The same goes for version numbers: version: 1.10 becomes the number 1.1.

Converting between them

Paste JSON to indent it, minify it or check it for errors, with the line and column of any mistake.

Format and validate JSON

Frequently asked questions

Is JSON better than XML?

For web APIs, yes, because it is smaller and faster to parse. XML is better when you need schemas, attributes or document markup.

Why is YAML used for config files?

It is easy to read and write by hand, and it supports comments, which JSON does not.

Can JSON have comments?

No. Standard JSON has no comment syntax. Use YAML, or a separate description field.

Is YAML a superset of JSON?

YAML 1.2 is designed so that almost all valid JSON is also valid YAML.

Related articles

← All guides