GUIDE

JSON, CSV and YAML: the small mistakes that break data files

The commas, quotes, separators and indentation that break JSON, CSV and YAML files, how to read the error messages, and why CSV cells may get an apostrophe.

By StillKit · Published

Most broken data files fail on a single character: a comma after the last item in JSON, a stray quote inside a CSV value, or one space too many in YAML. Each format has a few strict rules. A parser stops at the first point where the text stops making sense, usually right after the real mistake. Read an error message as "look here, and a little before".

JSON is strict about commas, quotes and comments

JSON puts every key and every string in straight double quotes. It allows no comma after the last item, no comments, and no values such as NaN, Infinity or undefined, while true, false and null must be lowercase. The JSON validator reports the first broken rule in your browser's own wording, so messages vary a little between browsers.

Common JSON mistakes and the messages they produce
MistakeExampleTypical message
Comma after the last item{"score": 92,}Expected double-quoted property name
Single or curly quotes{'name': 'Asha'}Expected property name or '}'
Missing comma between items{"name": "Asha" "score": 92}Expected ',' or '}' after property value
A comment"Asha" // ownerExpected ',' or '}' after property value
A value JSON does not have{"score": NaN}Unexpected token 'N'

Curly quotes usually arrive when JSON is pasted from a word processor or a chat app. Run the text through Normalize smart quotes first, which turns “ ” and ‘ ’ back into straight quotes, then validate again.

Two JSON surprises that pass validation

A key that appears twice is not an error. The validator accepts {"a": 1, "a": 2}, and the JSON formatter keeps only the last value, "a": 2. Other programs may keep the first value or reject the file, so search hand-merged files for repeated keys.

Large numbers lose digits. Browsers read every JSON number as a 64-bit floating-point value, which holds whole numbers exactly only up to 9,007,199,254,740,991. The formatter therefore turns 12345678901234567890 into 12345678901234567000. It also rewrites 1.50 as 1.5 and 1e2 as 100, which are the same values written differently. Keep long IDs, account numbers and similar codes as strings, in quotes.

Reading an error message

A JSON message such as "at position 29 (line 1 column 30)" gives the same spot twice: the position counts characters from 0, the column from 1. That spot is where the parser gave up, not necessarily where you went wrong. In {"name": "Asha, "score": 92} the closing quote after Asha is missing. The parser reads "Asha, " as a complete value, then complains at column 18, in front of score.

The YAML to JSON tool names a line and column and prints the line with a caret (^) under the spot, as in "All mapping items must start at the same column at line 3, column 1". It also treats warnings as errors, so an unknown tag such as !mytag stops the conversion instead of being skipped. StillKit's CSV messages name the rule that broke, such as "Unclosed CSV quote.", but not the line, so search the file for quote marks first.

CSV has one delimiter, and its quotes must pair up

In a CSV file a comma separates the fields. A field that contains a comma, a quote or a line break goes inside double quotes, and a quote within it is written twice: "She said ""hi""". The CSV to JSON tool takes the first row as the header. Each header must be filled in and different from the others, and every row must have as many fields as the header. Every value comes out as text, so a score of 92 becomes "92", because CSV has no types.

  • An inch mark in an unquoted value, as in 55" screen, gives "Unexpected quote in CSV."
  • Text after a closing quote, as in "TV" 55, gives "Unexpected content after a closing CSV quote."
  • A quote that never closes gives "Unclosed CSV quote."

Separators trip people up too. Spreadsheets set to a language that writes decimals with a comma often save "CSV" files with semicolons. StillKit reads commas only, so name;score arrives as a single column. Export the file again with commas rather than searching and replacing, which would also split decimal commas inside values. Line endings are forgiving: LF, CRLF and CR are all read, and an invisible byte order mark at the start is removed.

YAML uses indentation as its structure

Indent YAML with spaces only. A tab gives "Tabs are not allowed as indentation", and keys at the same level must start in exactly the same column. Some mistakes produce no error at all: in a list, an item indented one space too far joins the item above it. Three lines meant as a list of apple and pear become one item, "apple - pear", so compare the JSON output with what you expected.

A colon followed by a space starts a new key, even in the middle of a value. title: Note: read me fails with "Nested mappings are not allowed in compact mappings"; write title: "Note: read me" instead. Duplicate keys stop the conversion with "Map keys must be unique".

Unquoted values are given a type, sometimes not the one you meant. StillKit reads YAML version 1.2, in which zip: 01234 becomes the number 1234 and version: 1.10 becomes 1.1. Meanwhile yes, no, on, off and 12:30 stay as text. Older readers that follow YAML 1.1 treat unquoted yes, no, on and off as true or false, so the country code NO can turn into false. JSON to YAML quotes values such as "01234" and "1.10" to keep them as text, but leaves NO unquoted, which only YAML 1.2 readers keep as text. When in doubt, quote.

Text encoding: UTF-8, and why é turns into é

StillKit reads and writes UTF-8, the encoding the current JSON standard, RFC 8259, requires for JSON exchanged between systems. Trouble starts when a program reads UTF-8 bytes as an older Western European encoding: café appears as café. Some spreadsheet programs do this when you double-click a CSV file. Open it with the program's import command instead and choose UTF-8.

Formula injection, and the apostrophe in StillKit's CSV files

A spreadsheet treats a cell that starts with =, +, - or @ as a formula. If a CSV file holds text typed by strangers, such as web form entries or product reviews, someone can plant a formula in it. A formula can build a link that carries other cells' contents to a website, and it runs when the file is opened, not when the CSV is written.

JSON to CSV and the CSV file generator therefore put an apostrophe in front of any cell that starts with =, +, - or @. The same goes for a tab or line break at the start, and for any of these after leading spaces. =1+1 is written as '=1+1, which a spreadsheet shows as text. The guard is deliberately broad, so ordinary values get it too: -5 becomes '-5 and +94771234567 becomes '+94771234567. Remove the apostrophe in your spreadsheet if you need to calculate with such a column.

The apostrophe protects the people who open the file in a spreadsheet. It does not remove anything: the original text is still in the file, and any program that reads the CSV directly sees the apostrophe as part of the value.

TRY IT YOURSELF

Tools used in this guide

Find your next shortcut

Search loads only when needed. Your search stays on your device.

Browse all categories and sections →