jsonyamlformats

JSON, YAML, XML, and TOML: Data Format Guide

· Cosyslabs

Data serialization formats define how structured data is represented as text so it can be stored, transmitted, and parsed by different systems. Choosing the right format affects readability, performance, schema enforcement, and tooling. JSON, YAML, XML, and TOML each have distinct strengths that make them appropriate for different use cases.

JSON — JavaScript Object Notation

JSON is the dominant format for web APIs and data exchange. It maps directly to the data structures of most programming languages: objects, arrays, strings, numbers, booleans, and null.

{
  "user": {
    "id": "usr_123",
    "name": "Alice Chen",
    "email": "alice@example.com",
    "roles": ["admin", "editor"],
    "active": true,
    "score": 98.5,
    "metadata": null
  }
}

Strengths:

  • Native to JavaScript — no conversion needed in browser code
  • Ubiquitous API support across all languages
  • Compact and fast to parse
  • Strong tooling (jq, Prettier, JSON Schema)

Limitations:

  • No comments
  • No multiline strings (without escape sequences)
  • No trailing commas
  • All keys must be quoted strings
  • No date type — dates become strings

When to use: REST APIs, localStorage, inter-service communication, configuration consumed programmatically.

YAML — YAML Ain't Markup Language

YAML is a superset of JSON that adds significant human-readability features: comments, multiline strings, anchors for reuse, and unquoted strings.

# Docker Compose example
version: "3.9"
services:
  web:
    image: node:20-alpine
    ports:
      - "3000:3000"
    environment:
      NODE_ENV: production
      DATABASE_URL: &db_url "postgresql://user:pass@db:5432/app"
  worker:
    image: node:20-alpine
    environment:
      DATABASE_URL: *db_url  # reuse anchor

# Multiline string
description: |
  This is a multiline
  string that preserves
  line breaks.

YAML gotchas:

# These are all parsed as booleans in older YAML specs:
norway: NO        # false!
sweden: YES       # true!
on: true
off: false

# Version numbers become floats:
version: 1.10     # parsed as 1.1, not "1.10"
# Fix: quote them
version: "1.10"

When to use: CI/CD pipelines (GitHub Actions, Kubernetes), Docker Compose, Ansible, human-authored configuration files.

XML — Extensible Markup Language

XML is a verbose, self-describing format with strong schema support (XSD), namespaces, and transformation capabilities (XSLT). It remains dominant in enterprise systems, document formats, and legacy APIs.

<?xml version="1.0" encoding="UTF-8"?>
<users xmlns="https://api.example.com/schema">
  <user id="usr_123">
    <name>Alice Chen</name>
    <email>alice@example.com</email>
    <roles>
      <role>admin</role>
      <role>editor</role>
    </roles>
    <active>true</active>
    <!-- This is a comment -->
  </user>
</users>

XML-specific features:

  • Attributes vs elements (structural choice with different semantics)
  • Namespaces for combining vocabularies
  • XPath for querying
  • XSLT for transformations
  • DTD/XSD for schema validation
  • CDATA sections for raw text

When to use: SOAP web services, RSS/Atom feeds, SVG, Microsoft Office formats (OOXML), configuration in Java enterprise apps (Maven, Spring).

TOML — Tom's Obvious Minimal Language

TOML was designed specifically for application configuration files. It has an unambiguous spec and maps cleanly to hash tables (dictionaries).

# Cargo.toml (Rust package manager)
[package]
name = "my-app"
version = "1.0.0"
edition = "2021"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1", features = ["full"] }

[profile.release]
opt-level = 3
lto = true

[[servers]]
host = "192.168.1.1"
port = 8080

[[servers]]
host = "192.168.1.2"
port = 8081

Strengths:

  • Clear, unambiguous spec (no YAML "Norway problem")
  • Native types: integers, floats, booleans, dates, arrays, tables
  • Comments supported
  • Designed to be readable by non-programmers

When to use: application configuration (Rust/Cargo, Python pyproject.toml, Hugo, etc.), settings files that developers edit manually.

Format Comparison

FeatureJSONYAMLXMLTOML
CommentsNoYesYesYes
Multiline stringsEscapedYesCDATAYes
Trailing commasNoN/AN/AN/A
Schema validationJSON SchemaXSD/DTD
Human readabilityMediumHighLowHigh
Parse speedFastSlowMediumFast
Anchors/referencesNoYesXIncludeNo
API standardYesRareSOAPNo

Parsing in JavaScript

// JSON — built in
const obj = JSON.parse('{"name":"Alice"}');
const str = JSON.stringify(obj, null, 2); // pretty print

// YAML — js-yaml
import yaml from "js-yaml";
const obj = yaml.load("name: Alice\nroles:\n  - admin");
const str = yaml.dump(obj);

// TOML — @iarna/toml
import toml from "@iarna/toml";
const obj = toml.parse('[db]\nhost = "localhost"');
const str = toml.stringify(obj);

// XML — fast-xml-parser
import { XMLParser, XMLBuilder } from "fast-xml-parser";
const parser = new XMLParser({ ignoreAttributes: false });
const obj = parser.parse("<user id='1'><name>Alice</name></user>");

Tools