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.
Compare
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).
Tag text is characters until your schema or code casts types.
Reference
Key Differences
SyntaxJSON: { } / [ ] and quoted keys. XML: nested tags and optional attributes.
TypesJSON has string, number, boolean, null, object, array. XML element content is text until validated or cast.
SizeJSON is usually more compact—no repeated closing tag names.
NamespacesXML supports namespaces and attributes. JSON does not; use normal properties for metadata.
Web APIsJSON is the default for REST and browser fetch. XML remains common in SOAP and document formats.
ParsingIn JavaScript: JSON.parse / JSON.stringify. XML needs a DOM/parser API.
JSON Schema
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.
typeof shows string, number, and boolean after parse.
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.