TodayTools

JSON and YAML flashcards

The two text formats for data and config you will meet everywhere.

Donate
Beginner friendly, starts from zero
Section
How to study
0
JSON and YAMLStart from zero · 1 of 12

What are JSON and YAML, in one plain sentence?

They are two ways to write data as plain text so both people and programs can read it: JSON uses braces and quotes, YAML uses line breaks and spaces.

Same data, two handwriting styles.

All topics

JSON and YAML at a glance

JSON and YAML are two ways to write data as plain text that both people and programs can read.

Like a filled-in form: each label has an answer, written either in neat boxes (JSON) or as a tidy list (YAML).

Words you will meet

Key
The label part of a piece of data, like name
Value
What the label holds, like Karthik or 31
Object
A group of keys and values inside curly braces
Array
An ordered list inside square brackets
String
Text, written inside quotes
Boolean
A yes or no value: true or false
Indentation
Spaces at the start of a line that show nesting
Parse
When a program reads text and turns it into data

The steps

  1. 1. Start from zero (12 cards): What JSON and YAML are, the words you need, and your first file in each.
  2. 2. Core basics (15 cards): Write valid JSON and YAML by hand, spot the errors parsers reject, and read both in code.
  3. 3. Next steps (16 cards): YAML type traps, multi-line strings, anchors, big numbers, dates and the jq and yq tools.
  4. 4. Go deeper (16 cards): JSON Schema, YAML tags and safety, patches, streaming, serialisation edge cases and format choices.
All 12 cards in Start from zero
What are JSON and YAML, in one plain sentence?
They are two ways to write data as plain text so both people and programs can read it: JSON uses braces and quotes, YAML uses line breaks and spaces.
What was the problem before shared formats like these?
Every program made up its own file layout, so sharing data meant writing special code for each pair of programs. A common format lets any program read any other's data.
Where do you already meet JSON and YAML in daily life?
Your weather app gets today's forecast from a server as JSON. Settings files in apps and games, and the setup files of many websites, are JSON or YAML.
What everyday thing are they like?
A filled-in form with labelled boxes: 'Name: Karthik', 'City: Salem'. JSON is the form in neat ink with every box drawn; YAML is the same form as a tidy list.
What are a 'key' and a 'value', and what kinds of value are there?
A key is the label and a value is what it holds, as in "city": "Salem". A value can be a string (text in quotes), a number, a boolean (true or false) or null (empty on purpose).
What are an 'object' and an 'array'?
An object is a group of key and value pairs inside curly braces {}, like one contact card. An array is an ordered list inside square brackets [], like a shopping list.
What are 'indentation' and a 'comment'?
Indentation is spaces at the start of a line that show one thing sits inside another; YAML depends on it. A comment is a note for humans that programs ignore: YAML starts one with #, and plain JSON has none.
What do 'parse', 'config' and 'API' mean?
To parse is for a program to read the text and turn it into data it can use. A config is a settings file, and an API is the doorway an app uses to ask a server for data, which usually answers in JSON.
What do you need before starting?
A text editor (an app for plain text, like Notepad or VS Code) and a browser. Nothing to install; later cards use a terminal, the window where you type commands, and a little JavaScript or Python.
Your first tiny win: read this JSON.
An object with three keys. "Karthik" is a string, 31 is a number and true is a boolean; every key sits in double quotes.
The same person in YAML: what changed?
Each line is a key, a colon and a space, then the value. No braces, and simple text needs no quotes.
What will you be able to do after this topic?
Write valid JSON and YAML by hand, spot the mistakes that break them, turn one into the other, and read and write both from code.
JSON and YAML cheatsheet

JSON rules

  • { "key": "value" } Object, keys in double quotes
  • [1, "two", null] Array of any values
  • true / false / null Lower case only
  • -12.5e3 Number: no NaN, Infinity or leading zeros
  • "\n \" \\ \u00e9" String escapes
  • no trailing comma Last item has no comma
  • no comments Use JSONC or JSON5 if you need them

YAML syntax

  • key: value Mapping (space after colon)
  • - item List item
  • run: | Literal block, keeps newlines
  • text: >- Folded block, strip final newline
  • base: &base Anchor a node
  • <<: *base Merge an anchored map
  • --- Start a new document
  • zip: !!str 01234 Force a type with a tag

YAML traps to quote

  • country: "NO" yes/no/on/off are booleans in 1.1
  • version: "3.10" Else float 3.1
  • mode: "0755" Else octal 493 in 1.1
  • time: "12:30" Else base-60 number in 1.1
  • color: "#fff" Else a comment, value null
  • title: "Note: read" Colon space starts a mapping

In code

  • JSON.parse(text, reviver) Text to value, transform on the way
  • JSON.stringify(v, null, 2) Pretty-print with 2 spaces
  • JSON.stringify(v, ["id", "name"]) Only these keys
  • toJSON() { return {...} } Control a class's output
  • structuredClone(v) Deep copy that keeps Dates and Maps
  • yaml.safe_load(f) # Python Safe YAML parsing

Command line

  • jq . file.json Validate and pretty-print
  • jq -r '.items[].id' Raw values from an array
  • jq 'select(.qty > 5)' Filter objects
  • jq -s '.' events.jsonl JSON Lines into one array
  • yq -i '.spec.replicas = 3' d.yaml Edit YAML in place
  • yq -o=json f.yaml YAML to JSON
  • python3 -m json.tool f.json Validate with no install
  • yamllint -s . Strict YAML lint
Donate