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
jq installed 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 -100 or Python json.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) or jq with --stream.
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.
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:
jq one-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
jq and pandas.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' or try/except per 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
htop or similar).
- Re-run produces deterministic output.
- Success: JSON is turned into the exact shape needed for the next step, with validation protecting downstream systems.
References
1---2name: json-transformer3description: Transforms, reshapes, and validates JSON data structures. Use when normalizing API responses, converting nested JSON to flat tables, or validating JSON against a schema.4license: Apache-2.05compatibility: Claude Code, Codex, Gemini CLI6metadata:7 author: ai-skills8 version: "1.0"9 category: data-analytics10 tags: json, jq, json-schema, pandas, normalization, validation11---1213## Overview1415Transforms 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.1617## When to Use This Skill1819- Normalizing complex API responses (nested, inconsistent).20- Converting JSON logs or documents into tabular form for analysis.21- Validating incoming JSON payloads against a contract.22- The user provides JSON files or describes JSON transformation needs.2324## Prerequisites2526- `jq` installed for CLI transformations (highly recommended).27- Python with `pandas`, `jsonschema`, `jsonpath-ng` (optional but powerful).28- The JSON data (file, API response, or stdin).2930## Steps31321. **Inspect the JSON**:33 - `jq '.' file.json | head -100` or Python `json.load`.34 - Identify the shape (object, array of objects, deeply nested).35362. **Choose tool by use case**:37 - Simple reshaping / extraction: `jq`.38 - To pandas DataFrame: `pandas.json_normalize`.39 - Validation: JSON Schema + `jsonschema`.40 - Streaming large files: `ijson` (Python) or `jq` with `--stream`.41423. **Common jq patterns** (provide ready-to-use one-liners):43 - Extract array: `.items[]`44 - Flatten nested: `.user | {id, name, "address.city": .address.city}`45 - Filter + map.46 - Group by.47484. **pandas.json_normalize**:49 - `pd.json_normalize(data, record_path=['results'], meta=['query'])`.50 - `errors='ignore'` for inconsistent structures.51525. **JSON Schema validation**:53 - Write a schema (draft 2020-12 or 2019-09).54 - Validate in Python or with `ajv` (JS) / `check-jsonschema` (CLI).55 - Produce clear error messages with path to the bad field.56576. **Streaming large JSON**:58 - For NDJSON (newline-delimited): process line by line.59 - For large arrays: use `ijson.items(f, 'item')`.60617. **Output**:62 - `jq` one-liners or script.63 - Python function `transform_json(input_path) -> pd.DataFrame or list[dict]`.64 - JSON Schema file.65 - Example of before/after shape.6667## Examples6869- Normalize a complex GitHub API search response (nested user + repo objects) into a flat table using both `jq` and `pandas.json_normalize`.70- Validate incoming webhook payloads against a strict schema and quarantine bad ones.71- Stream-process a 500MB NDJSON log file and extract only error events.7273## Edge Cases & Error Handling7475- **Inconsistent nesting**: Use `errors='ignore'` or `try/except` per record + dead letter.76- **Huge files**: Never load the entire thing into memory.77- **Unicode / escaping issues**: Ensure UTF-8 throughout.7879## Verification80811. Run the transformation on sample input — output shape matches expectation.822. Validate a good and a deliberately bad JSON — errors are clear and point to the bad field.833. For large files: memory usage stays reasonable (monitor with `htop` or similar).844. Re-run produces deterministic output.855. Success: JSON is turned into the exact shape needed for the next step, with validation protecting downstream systems.8687## References8889- [jq Manual](https://jqlang.github.io/jq/manual/)90- [pandas.json_normalize](https://pandas.pydata.org/docs/reference/api/pandas.json_normalize.html)91- [JSON Schema](https://json-schema.org/)92- [ijson](https://github.com/ICRAR/ijson)93- [jsonpath-ng](https://github.com/h2non/jsonpath-ng)