Skip to content

YAML and CSV: the same data, different traps

YAML guesses types and CSV has no types at all.

Lesson 3 of 4 · about 3 minutes

YAML guesses, and sometimes guesses wrong

YAML is a superset of JSON meant to be written by hand: indentation instead of braces, comments allowed, quotes optional. The optional quotes are where it bites.

Unquoted values are interpreted. yes, no, on, off and true become booleans in YAML 1.1, which is what most parsers still implement. The country code for Norway is NO, so a list of country codes silently contains a boolean false. It is known as the Norway problem and it is a real outage, not a curiosity.

Numbers with leading zeros can be read as octal; a version like 1.20 becomes 1.2; a time like 12:30 can become a sexagesimal number. Quote anything you mean literally, and be especially careful with anything a person typed.

Indentation must be spaces. A tab is a parse error, which is unhelpfully invisible in most editors.

CSV has no types and barely a specification

A CSV cell is text. 007 is a string until something decides it is seven, which spreadsheets do enthusiastically — and a phone number starting +91 may be read as a formula. Leading zeros and long numbers are routinely destroyed on open, before anyone has edited anything.

The rules that do exist are about quoting: a field containing a comma, a quote or a newline must be quoted, and a quote inside a quoted field is doubled. Splitting a line on commas is therefore wrong for any file that might contain an address — use a parser, always.

And there is no header rule, no encoding rule and no agreement on the separator: plenty of "CSV" in Europe is semicolon-separated because the comma is a decimal point there. Anything reading CSV should be told what it is reading rather than guessing.

Worked examples

  • The Norway problem

    /countries:
      - IN
      - NO
      - GB/

    Against: NO parses as the boolean false

    Quote it — "NO" — and it stays a country. This has taken down real deployments.

  • A CSV field that cannot be split on commas

    /name,address
    "Rao, Asha","12 Main St, Pune"/

    Against: Two fields, each containing a comma

    Splitting on commas gives four values and silently corrupts every row with an address in it.

Check yourself

Practice questions written for this lesson — not past exam papers.

Practise every question in this subject

  1. Why can an unquoted NO in YAML cause an outage?

  2. Why is splitting a CSV line on commas wrong?

Try it