Lesson 8 — Comparisons

JSON vs XML

JSON and XML both represent structured data as text, but differ significantly in verbosity, readability, and how widely they're used in modern web development.

Side-by-Side Comparison

The same data, represented in both formats, highlights JSON's relative compactness — XML requires opening and closing tags for every element, while JSON uses lightweight punctuation.

JSON — A book record
{
  "title": "Learning JSON",
  "author": "J. Smith",
  "year": 2024
}

When XML Still Has an Edge

XML supports attributes, namespaces, and a mature ecosystem of validation (XSD) and transformation (XSLT) tools, which make it well-suited to certain document-centric formats and enterprise systems that predate JSON's rise — covered fully in the XML course.

Common Mistakes

  • Assuming JSON can fully replace XML in every context — some legacy enterprise systems and document formats are built around XML's specific capabilities.
  • Overlooking that JSON has no built-in support for comments, while XML does.
  • Choosing a format based on personal preference alone rather than what a specific API, system, or standard actually requires.
  • Forgetting that JSON has no native way to represent attributes the way XML elements can.

Professional Tip

For new web APIs, JSON is almost always the right default due to its readability and broad language support. Reach for XML specifically when you're integrating with a legacy system, or a standard, that already requires it.

Your Turn

Take a small XML document representing a person (name, age, city) and rewrite the same data as an equivalent JSON object.

Mini Quiz

Which of these is generally true when comparing JSON to XML?