C.W.K.
Stream
Lesson 07 of 10 · published

jq — JSON 처리

~13 min · jq, json

Level 0창 구경꾼
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

왜 JSON 이 줄 단위 파이프를 깼나

파이프는 줄당 한 레코드 가정. JSON 은 중첩. grep, cut, awk 는 키 따라 서브 오브젝트 못 들어가. jq 가 JSON 을 파이프 가능하게 만듦 — stdin 으로 읽어 작은 표현식 언어로 쿼리하고 JSON 또는 텍스트 출력.

Identity + pretty-print

jq '.' file.json = pretty-print + 색 (identity 필터). cat data.json | jq 가 'JSON 읽기 좋게' 표준 command.

필드 선택

  • jq '.name' — top-level name.
  • jq '.users[0].email' — 첫 user 의 email.
  • jq '.users[].email' — 모든 user 의 email (줄당 하나).
  • jq '.users | length' — 개수.
  • jq '.users | map(.email)' — email 배열.

필터링

  • jq '.users[] | select(.active == true)' — active user.
  • jq '.users[] | select(.age > 30) | .name' — 30 살 넘는 user 의 이름.

모양 바꾸기

  • jq '{id: .id, name: .name}' — 부분 추출 새 객체.
  • jq -r '.[].url' — raw 출력, 따옴표 X (curl 로 URL 파이프 넣을 때 좋음).
  • jq -c '.' — 한 줄 객체 (newline-delimited JSON, 스트리밍 친화 포맷).

실전 콤보

curl -s api.github.com/repos/cli/cli/issues \
  | jq '.[] | {n: .number, title}' | head
kubectl get pods -o json \
  | jq '.items[] | select(.status.phase != "Running") | .metadata.name'

둘 다 JSON 을 한 도구에서 받아 구조화 필터로 처리해 사람 읽기 결과로. 2026 의 파이프야.

Code

GitHub issue 를 깔끔한 표로·bash
curl -s https://api.github.com/repos/cli/cli/issues \
  | jq '.[] | [.number, .title] | @tsv' | head
값 빠르게 추출·bash
echo '{"name":"pippa","version":"1.0"}' | jq -r .name
# Multiple keys at once
echo '{"a":1,"b":2,"c":3}' | jq -r '.a, .c'

External links

Exercise

jq 설치: brew install jq. curl -s https://api.github.com/repos/jqlang/jq | jq '.stargazers_count, .forks_count'. JSON 배열에서 URL 추출: echo '[{"u":"a"},{"u":"b"}]' | jq -r '.[].u'.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.