0% complete

Files, data formats, and web APIs · Lesson 2 of 3

CSV, JSON, and data validation

Exchange structured data with standard-library parsers and validate its shape.

Lesson

1. Concept and mental model

CSV is a tabular interchange format, but it has details such as quoting, delimiters, and newlines that make manual string splitting unsafe. Use Python’s csv module so commas inside quoted fields are handled correctly. JSON represents objects, arrays, strings, numbers, booleans, and null. Use json.load() or json.loads() to parse it and json.dump() to serialize it; specify encoding and consider stable formatting for files reviewed by people.

2. How it works

Parsing checks syntax, not business validity. After reading JSON, verify the expected keys, value types, required fields, and allowed ranges before using it. A parsed object may be a list when your code expects a dictionary, or may contain unexpected values. Convert validated raw data into a clear internal representation and produce useful errors that identify the problem without exposing sensitive records.

3. Use it safely and effectively

Large data files may require streaming or a specialized library rather than reading the entire document into memory. Preserve identifiers that look numeric but are not quantities, such as account codes with leading zeros. Define how missing values, duplicate records, and malformed rows should be handled. Practice by reading a CSV of orders, validating each row, and writing a summary to JSON.

4. Syntax reference

import json

payload = '{"name":"Ada","active":true}'
record = json.loads(payload)
if not isinstance(record, dict) or not isinstance(record.get('active'), bool):
    raise ValueError('Invalid record')
print(record['name'])

5. Guided walkthrough

Parse external data into Python values, validate required fields and their types, then normalize it into the shape the rest of the application expects. Keep parsing errors distinct from business-rule errors so the user can correct malformed records.

6. Common mistakes to avoid

  • Treating valid JSON as valid application data without checking keys, types, or value limits.
  • Splitting CSV lines manually and breaking when quoted fields contain commas or newlines.

Visual guide

Validate structured data

  1. Step 01 Parse

    Decode valid CSV or JSON syntax.

  2. Step 02 Check

    Validate required fields and domain rules.

  3. Step 03 Transform

    Create trusted application data.

Code examples

Worked example
PYTHON
import json

payload = '{"name":"Ada","active":true}'
record = json.loads(payload)
if not isinstance(record, dict) or not isinstance(record.get('active'), bool):
    raise ValueError('Invalid record')
print(record['name'])
Expected output
Ada
Validate required JSON fields
PYTHON
payload = {'name': 'Ravi', 'active': True}
if not isinstance(payload.get('name'), str):
    raise ValueError('name must be text')
print(payload['name'])
Expected output
Ravi
Write CSV with the standard library
PYTHON
import csv

rows = [{'name': 'Ada', 'score': 98}]
with open('scores.csv', 'w', newline='', encoding='utf-8') as file:
    writer = csv.DictWriter(file, fieldnames=['name', 'score'])
    writer.writeheader()
    writer.writerows(rows)
Expected output
Creates scores.csv with a header and one data row.

Key points

  • Exchange structured data with standard-library parsers and validate its shape.
  • Prefer explicit, testable behavior over hidden side effects.
  • Validate assumptions at system boundaries and handle failure deliberately.

Check your understanding

Practice

Does successful JSON parsing prove the data meets your business rules?

Your learning progress

Finish this lesson