File formats: text, CSV, and JSON
Programs constantly exchange data with files and other programs. Choosing and handling the right format makes data easy to store, share, and process correctly.
Text and binary files
Text files store characters in an encoding such as UTF-8 and can be read in any editor. Binary files, such as images, compiled programs, and spreadsheets in native formats, store bytes whose meaning depends on the format. Opening a binary file as text produces gibberish; programs must read each format appropriately.
CSV
Comma-separated values files store tables as lines of text, one row per line, with fields separated by commas and an optional header row. They are simple and open in spreadsheets, but tricky cases arise: commas or line breaks inside fields require quoting, and there are no data types, so everything is text until converted. Always use a CSV library rather than splitting on commas by hand.
JSON
JavaScript Object Notation (JSON) represents structured data as text using objects (key-value pairs in braces), arrays (ordered lists in brackets), strings, numbers, true, false, and null. Example: {"name": "Ana", "scores": [90, 85], "active": true}. JSON is human-readable, language-independent, and the dominant format for web APIs and configuration.
Parsing and serialising JSON
Parsing turns JSON text into a program's data structures; serialising does the reverse. In Python, json.loads(text) gives dictionaries and lists, and json.dumps(data, indent=2) produces formatted text. In JavaScript, JSON.parse and JSON.stringify do the same. JSON requires double quotes and allows no trailing commas or comments, common sources of parse errors.
Other formats
XML uses nested tags and was dominant before JSON for documents and data exchange; it remains common in some industries. YAML is a human-friendly format popular for configuration files, using indentation. Parquet is a compressed, column-based binary format used for large analytical datasets. SQLite stores a whole relational database in a single file.
Validating data
Data from files and users can be missing, malformed, or malicious. Check that required fields exist, types are correct, and values lie in sensible ranges before using them. JSON Schema describes the expected structure of JSON documents, and libraries such as Pydantic in Python validate and convert data automatically, producing clear error messages.
Encoding pitfalls
Text must be read with the encoding it was written in. Reading UTF-8 text as a legacy encoding turns accented characters into garbled sequences such as é instead of é. Specify encoding explicitly, prefer UTF-8 everywhere, and be aware that Excel and older Windows tools sometimes use other encodings or add a byte-order mark.
Worked example
Converting a CSV of products to JSON in Python: import csv, json; with open("products.csv", encoding="utf-8") as f: rows = list(csv.DictReader(f)); for r in rows: r["price"] = float(r["price"]); with open("products.json", "w", encoding="utf-8") as f: json.dump(rows, f, indent=2). Converting price to float matters: CSV values are all strings, so without it JSON would store "4.50" as text.
Key takeaways
Text files hold encoded characters; binary files hold format-specific bytes. CSV stores tables simply but needs quoting rules and has no types; use a library. JSON represents objects, arrays, and primitives and dominates web APIs; parse with json.loads or JSON.parse and serialise with dumps or stringify. XML, YAML, Parquet, and SQLite serve other needs. Validate external data with checks, JSON Schema, or Pydantic. Specify UTF-8 to avoid garbled text.
Flashcard
Characters in an encoding
Check your understanding
Why does {'name': 'Ana',} fail to parse as JSON?
