C.W.K.
Stream
Lesson 02 of 08 · published

변환 도구 — yq, jq, dasel, gron

~12 min · interop, yq, jq, tools

Level 0평문
0 XP0/64 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

네 포맷 잇는 CLI toolchain

jq — JSON 전용 쿼리 언어

개척자. jq 가 JSON 파싱, 쿼리 적용, JSON emit. 그 필터 문법 (.foo[], map, select) 이 다른 거의 lingua franca 됨.

yq — 같은 문법, 네 포맷 다

Mike Farah 의 yq 가 jq 의 필터 언어를 YAML, JSON, TOML, XML, CSV 에 적용. yq -p toml -o yaml file.toml 가 TOML 읽고 YAML emit. 가장 좋은 범용 변환 도구.

dasel — 여러 포맷 가로지르는 selector

dasel 이 통합 selector 문법 (.config.server.port) 사용, JSON/YAML/TOML/XML/CSV 지원. 단순 lookup 엔 yq 보다 가벼움; 변환엔 비슷.

gron — grep 위해 JSON 평탄화

gron 이 중첩 JSON 을 줄당 한 할당으로: json.users[0].name = "Pippa";. 그 후 grep 가 JSON 에 기대대로 작동. gron --ungron 가 원본 재구성. 깊이 중첩된 API 응답에서 필드 찾기-그-다음-추출에 외과적.

원칙: jq 의 필터 언어 한 번 배워; YAML/TOML 에 yq 통해 재사용. 투자가 config audit, log 탐험, CI 파이프라인 접착제 가로질러 갚음. Python 잡고 YAML 파일 주무르려는 순간 yq 존재 잊은 것 같음.

Code

yq — 필요한 모든 변환·bash
# YAML → JSON
yq -o json file.yaml

# JSON → YAML
yq -p json -o yaml file.json

# TOML → YAML
yq -p toml -o yaml pyproject.toml

# YAML → TOML
yq -p yaml -o toml file.yaml

# JSON → TOML
yq -p json -o toml file.json

# 라운드트립 — 제자리 pretty-print
yq -i . config.yaml
jq + yq 조합·bash
# multi-document YAML 가로질러 모든 replicas 찾기
yq '.. | select(has("replicas")) | .replicas' deploy/*.yaml

# label 가진 open PR 만 PR JSON 필터
gh pr list --json number,title,labels | \
  jq '[.[] | select(.labels[]?.name == "ready-for-review")]'

# pyproject.toml [project] block 을 JSON 으로 변환
yq -p toml -o json '.project' pyproject.toml
dasel — 빠른 lookup·bash
# 값 가져오기
dasel -f config.yaml '.server.port'

# 값 설정 (제자리)
dasel put -f config.yaml -v 9000 '.server.port'

# -r/-w 플래그로 변환
dasel -r yaml -w json < config.yaml > config.json
gron — 깊이 중첩 JSON grep·bash
# 평탄화
gh api repos/cwk/foo | gron | head
# json.full_name = "cwk/foo";
# json.private = false;
# json.owner.login = "cwk";

# 응답의 모든 URL 필드 찾기:
gh api repos/cwk/foo | gron | grep '_url'

# gron 출력 편집 후 ungron 으로 JSON 복원
gh api repos/cwk/foo | gron > flat.txt
# (flat.txt 편집)
gron --ungron < flat.txt > modified.json

External links

Exercise

실제 config (pyproject.toml, Ansible inventory, Kubernetes manifest) 골라. yq 로 다른 세 포맷 각각으로 변환. 결과 diff. 잃는 거 봐 (주석, 일부 라운드트립의 키 순서). 가장 자주 쓴 변환 incantation 을 dotfiles 의 shell 함수로 저장.

Progress

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

댓글 0

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

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