JSON, XML and CSV
Level 2 · Data formats
Three plain text ways to write down data. How JSON uses names and values, how XML uses tags, and how CSV holds a table, with the rules each one breaks on.
Course contentsLesson 3 of 11
Level 1 · Files and formats
Level 2 · Data formats
Level 3 · Config and docs files
Level 4 · Characters and escapes
Level 5 · IDs, dates and patterns
After this lesson you can
- Read and write a small JSON file
- Spot the same data in XML and CSV
- Pick the format that fits the data
13 mincovers 3 glossary entries
Worth reading first: Plain Text and Binary Files
How JSON, XML and CSV write the same data
JSON, XML and CSV are three plain text formats for data. JSON writes names and values in curly brackets, like "age": 36. XML wraps each value in tags, like <age>36</age>. CSV is a table, one row per line, with commas between the columns. Most web services send data as JSON, and its rules fit on one page.
JSON can hold text, numbers, true or false, null, lists in square brackets and groups in curly brackets. Groups can sit inside groups. Text must be in double quotes, and the names too. Almost every language can read and write it.
JSON is strict, and two slips break most files. A comma after the last item is not allowed. Comments are not allowed either. One stray comma, and the whole file fails to load, not just that line.
XML came first, in 1998. Each value sits between an opening tag and a closing tag, so every name is written twice. That makes it longer than JSON. It is still inside office files, news feeds, Android screens and many older business systems.
XML is strict too. A missing closing tag makes the whole file unreadable. HTML looks a lot like XML, with tags in angle brackets. The difference is that HTML is for pages people see, and XML is for data.
CSV is the simplest of the three. The first line usually names the columns, and every line after it is one row. Every spreadsheet app opens it. The catch is that CSV has no types, so a number and a word look the same.
A value with a comma in it breaks a CSV row into too many columns. Put that value in double quotes to keep it whole. Pick CSV for flat tables, JSON for nested data, and XML when a system asks for it.
Which one to pick
{
"name": "Emma",
"age": 36,
"admin": true,
"skills": ["Python", "Git"]
}Worked example
Emma's file will not load
Emma adds a fifth setting to a JSON file of 30 lines, and the app stops starting. The error says unexpected character at line 31.
Line 30 is the last item, and it ends with a comma left over from a copy. She deletes the 1 comma, and the app starts.
The words for this lesson3
Check yourself
Why does this JSON fail to load: {"a": 1, "b": 2,}
Finished this lesson?
Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.