jq 치트시트
jq 1.8 기준으로 검증된 jq JSON 프로세서 필터, 파이프, 객체 생성, 배열, select, map, group_by, reduce 및 CLI 플래그.
동작 원리
Every jq filter transforms an input JSON stream into zero or more output JSON values. Dot (`.`) is the identity filter; pipes (`|`) pass each output of the left filter into the right filter, and `.[]` explodes an array or object into a stream of items so you can filter, map and re-wrap them with `[...]`.
서바이벌 키트
가장 먼저 배워야 할 몇 가지 명령어입니다.
40개 명령어
Inspect & format JSON
Start with `.` to validate and pretty-print JSON, or `-r`/`-c` to adapt formatting for downstream tools.
공식 문서: Invoking jq- 기본
Validate and pretty-print JSON with syntax highlighting.
jq . <file.json>사용 시점
Quickly format minified API responses or confirm JSON syntax from the terminal.
상세 분석
- .
- identity filter: passes the whole JSON value unchanged
예제
echo '{"b":2,"a":1}' | jq -c .결과
{"b":2,"a":1}대체 방법
- jq -S . <file.json> (sort object keys alphabetically)
참고
Access fields & arrays
Chain property names with `.a.b`, index arrays with `.[0]`, and append `?` to skip missing/null errors.
공식 문서: Basic filtersFilter & transform
Combine `select(cond)` to drop items and `map(...)` or `[...]` to transform the remaining ones.
공식 문서: Builtin operators & functionsSort, group & aggregate
Group records by a key, deduplicate, compute totals, or flatten nested arrays.
공식 문서: Sorting and groupingCLI variables, CSV & shell interop
Pass shell parameters safely with `--arg` / `--argjson` and export tables directly with `@csv` or `@tsv`.
공식 문서: Format strings and escapingKitLocal 도구에서 사용해 보기
용어집
- Filter
- An expression in jq that takes a JSON value and outputs zero or more values.
- Stream
- Multiple values produced by `.[]` or `,` before wrapping back into an array with `[...]`.
- Raw output (-r)
- Outputs strings without JSON surrounding quotes — essential when feeding bash or grep.
- Compact (-c)
- Prints each JSON object on a single line (NDJSON / JSON Lines).