| name | json-yaml |
| category | data |
| description | Parse, query, transform, validate, and convert JSON and YAML data using jq and fy (fast-yaml). Use when the user asks to filter JSON arrays, extract nested fields, reshape data structures, convert between JSON and YAML formats, validate or lint YAML files, pretty-print or minify JSON, format YAML, or perform complex data transformations on structured data.
|
| license | MIT |
| compatibility | Requires jq. Requires fy (fast-yaml) for YAML processing (cargo install fast-yaml-cli) |
| metadata | {"author":"zeph","version":"1.0"} |
JSON & YAML Processing with jq and fy
Quick Reference
| Action | Command |
|---|
| Pretty-print JSON | jq . file.json |
| Minify JSON | jq -c . file.json |
| Extract field | jq '.name' file.json |
| Extract raw string | jq -r '.name' file.json |
| Validate YAML | fy parse file.yaml |
| Format YAML | fy format -i file.yaml |
| Lint YAML | fy lint file.yaml |
| YAML to JSON | fy convert json file.yaml |
| JSON to YAML | fy convert yaml file.json |
| Validate JSON | jq empty file.json |
jq Basics
Identity and Field Access
cat data.json | jq .
jq '.user.address.city' data.json
jq '.[0]' data.json
jq '.items[2]' data.json
jq '.name, .age' data.json
jq '.missing?' data.json
String Interpolation
jq -r '"Name: \(.name), Age: \(.age)"' data.json
jq -r '.[] | "User \(.id): \(.email)"' users.json
Raw Output
jq -r '.name' data.json
echo "hello" | jq -R .
jq -r0 '.[]' list.json | xargs -0 -I{} echo "{}"
jq Filters
select — Filter by Condition
jq '.[] | select(.age > 30)' users.json
jq '.[] | select(.name == "Alice")' users.json
jq '.[] | select(.tags | contains(["admin"]))' users.json
jq '.[] | select(.email | test("@gmail\\.com$"))' users.json
jq '.[] | select(.address != null)' users.json
jq '.[] | select(.age > 25 and .status == "active")' users.json
jq '.[] | select(.role == "admin" or .role == "superadmin")' users.json
map — Transform Each Element
jq '[.[] | .name]' users.json
jq 'map(.name)' users.json
jq 'map({name: .name, upper: (.name | ascii_upcase)})' users.json
jq 'map(select(.active) | .email)' users.json
jq 'map_values(. + 1)' '{"a": 1, "b": 2}'
reduce — Accumulate a Result
jq 'reduce .[] as $x (0; . + $x)' numbers.json
jq 'reduce .[] as $item ({}; . + {($item.id | tostring): $item.name})' items.json
jq 'reduce .[] as $s (""; . + $s + " ")' strings.json
jq 'reduce .[] as $item ({}; .[$item.category] += 1)' items.json
group_by — Group Elements
jq 'group_by(.department)' employees.json
jq 'group_by(.status) | map({status: .[0].status, count: length})' items.json
jq 'group_by(.team) | map({team: .[0].team, members: map(.name)})' people.json
sort_by — Sort Elements
jq 'sort_by(.name)' users.json
jq 'sort_by(.age) | reverse' users.json
jq 'sort_by(.department, .name)' employees.json
jq 'sort_by(-.score)' scores.json
jq 'unique_by(.email)' users.json
jq Object Construction
jq '.[] | {name, email}' users.json
jq '.[] | {user_name: .name, user_email: .email}' users.json
jq '.[] | . + {full_name: "\(.first) \(.last)"}' users.json
jq '. as $a | input as $b | $a * $b' file1.json file2.json
jq 'del(.password, .secret)' user.json
jq 'to_entries' object.json
jq 'to_entries | map({key, value})' object.json
jq 'from_entries' entries.json
jq Array Operations
jq 'length' array.json
jq '.items | length' data.json
jq 'flatten' nested.json
jq 'flatten(1)' nested.json
jq 'add' numbers.json
jq 'min' numbers.json
jq 'max_by(.score)' items.json
jq 'min_by(.price)' products.json
jq 'first' array.json
jq 'last' array.json
jq 'nth(3)' array.json
jq 'limit(5; .[])' array.json
jq 'index("needle")' array.json
jq '[.a, .b] | combinations' data.json
jq Conditionals
jq '.[] | if .age >= 18 then "adult" else "minor" end' people.json
jq '.name // "unknown"' data.json
jq 'try .foo.bar catch "not found"' data.json
jq Type Functions
jq '.[] | type' data.json
jq '.[] | select(type == "string")' data.json
jq '.count | tonumber' data.json
jq '.id | tostring' data.json
jq '.price | floor' item.json
jq '.price | ceil' item.json
jq '.price | round' item.json
jq '.values | add / length' data.json
jq String Functions
jq '.name | length' data.json
jq '.name | ascii_upcase' data.json
jq '.name | ascii_downcase' data.json
jq '.path | split("/")' data.json
jq '.tags | join(", ")' data.json
jq '.email | test("^[a-z]+@")' data.json
jq '.line | match("(\\w+)=(\\w+)")' data.json
jq '.line | capture("(?<key>\\w+)=(?<val>\\w+)")' data.json
jq '.text | gsub("foo"; "bar")' data.json
jq '.text | sub("^prefix_"; "")' data.json
jq '.name | startswith("Dr.")' data.json
jq '.file | endswith(".json")' data.json
jq '.path | ltrimstr("/api/v1")' data.json
jq with Pipes and Shell
curl -s "https://api.example.com/users" | jq '.data[].name'
name=$(jq -r '.name' config.json)
jq -r '.[] | .id' items.json | while read -r id; do echo "Processing $id"; done
echo '{"a":1}' | jq '.a'
jq -s '.[0] * .[1]' defaults.json overrides.json
jq -n --arg name "$NAME" --arg email "$EMAIL" '{name: $name, email: $email}'
jq --arg threshold "$THRESH" '.[] | select(.score > ($threshold | tonumber))' data.json
fy (fast-yaml) — YAML Processing
All YAML operations use fy (fast-yaml CLI). Install: cargo install fast-yaml-cli.
Validate YAML
fy parse config.yaml
fy parse config.yaml values.yaml
fy parse src/
Format YAML
fy format config.yaml
fy format -i config.yaml
fy format -i src/
fy format -i "**/*.yaml"
fy format -i -j 8 project/
Lint YAML
fy lint config.yaml
fy lint --exclude "tests/**" .
fy lint src/
Convert Between Formats
fy convert json config.yaml
fy convert yaml data.json
fy convert json config.yaml > config.json
fy convert yaml data.json > data.yaml
Batch Operations
fy activates batch mode automatically for directories, glob patterns, or multiple files:
- Respects
.gitignore exclusions
- Supports include/exclude patterns
- Parallel worker configuration via
-j flag
- 6-15x speedup on multi-file operations
fy format -i -j 4 .
fy lint --exclude "tests/fixtures/**" .
fy parse src/ config/ deploy/
Workflow: Edit and Validate
After editing any YAML file, always validate with fy:
fy parse config.yaml
fy format -i config.yaml
Important Notes
jq uses single quotes for the filter expression to avoid shell expansion
- Use
-r for raw output (no quotes around strings) when piping to other commands
- Use
-e to set exit code based on output (null/false = exit 1)
- Use
-c for compact (single-line) output, useful for line-by-line processing
- Use
--slurpfile to load a JSON file into a variable: jq --slurpfile cfg config.json '...'
- When constructing JSON with
jq -n, use --arg for strings and --argjson for numbers/booleans
- Always use
fy for YAML validation, formatting, and conversion — never edit YAML manually without validating with fy afterward
fy supports YAML 1.2.2 specification