Overview
Transforms and validates JSON data using jq (CLI), Python (json, jsonpath-ng, pandas.json_normalize), and JSON Schema (jsonschema library). Covers common patterns: flattening nested objects/arrays, extracting specific fields, array of objects → table, schema validation, streaming large JSON files, and producing clean output for databases or downstream processing.
When to Use This Skill
- Normalizing complex API responses (nested, inconsistent).
- Converting JSON logs or documents into tabular form for analysis.
- Validating incoming JSON payloads against a contract.
- The user provides JSON files or describes JSON transformation needs.
Prerequisites
jqinstalled for CLI transformations (highly recommended).- Python with
pandas,jsonschema,jsonpath-ng(optional but powerful). - The JSON data (file, API response, or stdin).
Steps
Inspect the JSON:
jq '.' file.json | head -100or Pythonjson.load.- Identify the shape (object, array of objects, deeply nested).
Choose tool by use case:
- Simple reshaping / extraction:
jq. - To pandas DataFrame:
pandas.json_normalize. - Validation: JSON Schema +
jsonschema. - Streaming large files:
ijson(Python) orjqwith--stream.
- Simple reshaping / extraction:
Common jq patterns (provide ready-to-use one-liners):
- Extract array:
.items[] - Flatten nested:
.user | {id, name, "address.city": .address.city} - Filter + map.
- Group by.
- Extract array:
pandas.json_normalize:
pd.json_normalize(data, record_path=['results'], meta=['query']).errors='ignore'for inconsistent structures.
JSON Schema validation:
- Write a schema (draft 2020-12 or 2019-09).
- Validate in Python or with
ajv(JS) /check-jsonschema(CLI). - Produce clear error messages with path to the bad field.
Streaming large JSON:
- For NDJSON (newline-delimited): process line by line.
- For large arrays: use
ijson.items(f, 'item').
Output:
jqone-liners or script.- Python function
transform_json(input_path) -> pd.DataFrame or list[dict]. - JSON Schema file.
- Example of before/after shape.
Examples
- Normalize a complex GitHub API search response (nested user + repo objects) into a flat table using both
jqandpandas.json_normalize. - Validate incoming webhook payloads against a strict schema and quarantine bad ones.
- Stream-process a 500MB NDJSON log file and extract only error events.
Edge Cases & Error Handling
- Inconsistent nesting: Use
errors='ignore'ortry/exceptper record + dead letter. - Huge files: Never load the entire thing into memory.
- Unicode / escaping issues: Ensure UTF-8 throughout.
Verification
- Run the transformation on sample input — output shape matches expectation.
- Validate a good and a deliberately bad JSON — errors are clear and point to the bad field.
- For large files: memory usage stays reasonable (monitor with
htopor similar). - Re-run produces deterministic output.
- Success: JSON is turned into the exact shape needed for the next step, with validation protecting downstream systems.