androidengineers.Book a session

Python 07 · Files and interchange formats

JSON, CSV, and schema validation

articleSelf-paced

Learn the concept

JSON represents objects, arrays, strings, numbers, Booleans, and null. Python’s json module converts these into familiar values, but parsing success does not prove the data matches your application schema. Check required keys and types afterwards.

CSV is a tabular interchange format with quoting rules. Use the csv module instead of splitting lines on commas, because a quoted field can contain a comma or newline. CSV values generally arrive as strings and need deliberate conversion.

Keep parsing, validation, and transformation separate so you can identify whether a failure came from malformed syntax or invalid business data. Preserve source row numbers in error reports. Never use Python evaluation to parse an untrusted file.

Run and inspect

import csv, io, json

payload = json.loads('{"limit": 3}')
assert payload["limit"] == 3
rows = list(csv.DictReader(io.StringIO('name,count\n"Ada, team",2\n')))
assert rows[0]["name"] == "Ada, team"
assert int(rows[0]["count"]) == 2

Your exercise

Load a CSV with one invalid count and a JSON object missing a required field. Produce a valid-record list and an error list with source positions.

Check your understanding

Quoted commas are handled correctly and malformed records are not silently accepted or dropped.

YOUR LEARNING JOURNEY

0 of 118 available lessons completed

Progress saved in this browser. No account needed.
JSON, CSV, and schema validation | Agentic AI | Android Engineers