Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 39 ยท Getting Started

What Are Data Formats?

Learn what a data format is, why programs need one, and how JSON, XML, YAML and Markdown differ, with the same record written four ways and run in your browser.

Programs keep their data in memory as objects, arrays, numbers and strings. Memory vanishes when the program ends, and it cannot be sent through a cable or saved in a file as it is. To store or share data, a program must turn it into a flat sequence of characters or bytes. That sequence follows agreed rules, and the rules are called a data format.

This course covers the four text formats you will meet most often: JSON, XML, YAML and Markdown, plus close relatives such as TOML and CSV. By the end you will be able to read, write, validate, transform and safely parse all of them.

The examples run on a Node.js 24 runner. Node has built-in support for JSON only, so the XML, YAML and schema lessons use short hand-written helper code, clearly labelled as a stand-in for a real library.

Serialization and parsing

Turning in-memory data into text is serialization (also called encoding or stringifying). Turning the text back into data is parsing (decoding). Both sides must agree on the format, otherwise the text is just noise.

flowchart LR
  A["Object in memory"] -->|"serialize"| B["Text or bytes<br/>(file, network)"]
  B -->|"parse"| C["Object in memory<br/>(another program)"]

A data format is the contract between the program that writes the text and the program that reads it.

Two properties matter for every format you will study:

  • Self-describing: does the text carry names for its values ("age": 30), or only positions (30)?
  • Human-readable: can a person open the file in a plain editor and understand it?

All formats in this course are text and mostly self-describing. Binary formats such as Protocol Buffers or MessagePack are smaller and faster but unreadable without tools.

One record, four formats

Here is the same small record written as JSON, XML, YAML and CSV. The code below builds each version from one JavaScript object with tiny hand-written writers. They are a stand-in that shows the shapes; real projects use proper libraries.

Node.js The same record in four formats

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice what each format does well:

  • JSON is compact and maps directly onto objects and arrays.
  • XML repeats every name in a closing tag, which costs bytes but supports attributes, namespaces and mixed text.
  • YAML uses indentation instead of brackets, which reads nicely but is sensitive to spaces.
  • CSV is a flat table; the list of authors had to be squeezed into one cell.

Round trips

A good format survives a round trip: serialize, parse, and get back an equal value. JSON does this for plain data, but not for everything.

Node.js A JSON round trip and what it loses

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Dates became strings, undefined disappeared and NaN became null. The format only knows a few types, so everything else must be converted by you. JSON types and numbers goes into the details.

Where you will meet each format

Format Typical use
JSON Web APIs, browser storage, config files such as package.json
XML Documents, RSS/Atom feeds, SVG, office files, SOAP, Android layouts
YAML Configuration: Docker Compose, Kubernetes, CI pipelines
Markdown Documentation, README files, blog posts, chat messages
TOML Config files such as pyproject.toml and Cargo.toml
CSV Spreadsheets, exports, data science imports

Choosing between them is the subject of Choosing the right format.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. What is serialization?

    Serialization writes data out in a format; the opposite step is called parsing.
  2. 2. Which is true about JSON compared with XML for the same record?

    XML needs closing tags, so it is usually longer. JSON has arrays but no date type.
  3. 3. After JSON.stringify and JSON.parse, what happens to a Date object?

    Date has a toJSON method that returns an ISO string, and parsing leaves it a string.

Summary

  • A data format is a text (or binary) contract for storing and exchanging data.
  • Serialization writes data out; parsing reads it back; a good format survives a round trip.
  • JSON, XML, YAML, Markdown, TOML and CSV each fit different jobs.
  • Formats know few types, so dates, undefined and special numbers need care.

Next: Choosing the right format