JSON vs XML

Beginner
5 min read
Updated: Nov 2026
3 examples

Definition and Usage

JSON and XML both store structured data as text. In this JSON tutorial, the focus is why JSON became the usual choice for web APIs: smaller payloads, typed values, and a direct map to objects in JavaScript.

XML still wins where tags, attributes, namespaces, or document markup are required (SOAP, RSS, SVG, Office). For REST-style JSON APIs, learn JSON first.

Beginner path.

Compare the same record below, note the differences, then practice with the examples. Next: JSON Syntax.

Same Data, Two Formats

One person record. JSON uses braces and typed tokens; XML uses open/close tags (and every element value starts as text).

JSON
JSON
{
  "name": "Ada",
  "age": 36,
  "active": true,
  "skills": ["math", "code"]
}

age is a number and active is a boolean in the grammar itself.

XML
XML
<person>
  <name>Ada</name>
  <age>36</age>
  <active>true</active>
  <skills>
    <skill>math</skill>
    <skill>code</skill>
  </skills>
</person>

Tag text is characters until your schema or code casts types.

Key Differences

  • Syntax JSON: { } / [ ] and quoted keys. XML: nested tags and optional attributes.
  • Types JSON has string, number, boolean, null, object, array. XML element content is text until validated or cast.
  • Size JSON is usually more compact—no repeated closing tag names.
  • Namespaces XML supports namespaces and attributes. JSON does not; use normal properties for metadata.
  • Web APIs JSON is the default for REST and browser fetch. XML remains common in SOAP and document formats.
  • Parsing In JavaScript: JSON.parse / JSON.stringify. XML needs a DOM/parser API.

Schemas for JSON

If you know XSD for XML: JSON Schema plays a similar role for JSON. A schema is itself JSON that declares expected types and structure so tools can validate payloads.

JSON
{
  "type": "object",
  "required": ["name", "age"],
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer" },
    "active": { "type": "boolean" },
    "skills": {
      "type": "array",
      "items": { "type": "string" }
    }
  }
}

Examples

Use View Output for a quick result, or Try It Yourself to edit and run in the browser.

Example 1 — Parse the JSON Record

Same person data as above—ready for JSON.parse.

JSON
const text = `{
  "name": "Ada",
  "age": 36,
  "active": true,
  "skills": ["math", "code"]
}`;

const person = JSON.parse(text);
console.log(person.name);
console.log(person.age);
console.log(person.skills[0]);
Try It Yourself

How It Works

  1. JSON text goes into JSON.parse.
  2. You get a normal JavaScript object.
  3. Read fields with dot and index access—no XML DOM walk.

Example 2 — Size vs XML-Style Markup

Count characters for the same facts written as JSON and as tag markup.

JSON
const jsonText = JSON.stringify({
  name: "Ada",
  age: 36,
  active: true
});

const xmlText =
  "<person>" +
  "<name>Ada</name>" +
  "<age>36</age>" +
  "<active>true</active>" +
  "</person>";

console.log("JSON length:", jsonText.length);
console.log("XML length:", xmlText.length);
console.log(jsonText);
Try It Yourself

How It Works

  1. JSON.stringify builds compact JSON text.
  2. XML repeats tag names on open and close.
  3. Fewer characters often means less bandwidth for APIs.

Example 3 — Typed Values in JSON

After parse, numbers and booleans keep their types—unlike raw XML text nodes.

JSON
const person = JSON.parse(`{
  "name": "Ada",
  "age": 36,
  "active": true
}`);

console.log(typeof person.name);   // string
console.log(typeof person.age);    // number
console.log(typeof person.active); // boolean
console.log(person.age + 1);       // 37
Try It Yourself

How It Works

  1. JSON tokens carry type in the text.
  2. typeof shows string, number, and boolean after parse.
  3. You can do math on age without casting a string first.

Frequently Asked Questions

For most public HTTP APIs, JSON is the default because it is compact and maps to objects in JavaScript and many other languages. XML remains common in SOAP, RSS/Atom, SVG, Office formats, and enterprise XSD pipelines.
For the same logical record, JSON is usually smaller because it does not repeat opening and closing tag names. Exact size depends on field names and how verbose the XML tags are.
No. JSON has no attributes or namespaces. Everything is keys and values in objects and arrays. Put metadata in normal properties.
Use JSON Schema. A schema is itself JSON that describes expected types, required fields, and structure—similar in role to XSD for XML.

Next: JSON Syntax

Learn how JSON objects and arrays are written.

JSON Syntax tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful