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

공백, pretty-print, compact

~10 min · json, whitespace, pretty-print, jq

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

공백은 장식; 파서는 무시

JSON 은 토큰 사이 공백 무시. {"a":1}

{
  "a": 1
}

는 같은 값으로 파싱. 공백은 사람용 — 청중에 맞춰 골라.

JSON 두 형태

  • Compact — 공백 없음, 최소 byte. wire 에서 사용 (HTTP 응답, 로그, 메시지 큐). Nginx 로그, gzip 파이프라인, 네트워크 대역폭 다 혜택.
  • Pretty-printed — 2 칸 indent, 키당 한 줄. 소스 파일 (config), 문서, 디버깅에서 사용. git 에서 깔끔하게 diff.

알아야 할 도구

모든 언어가 pretty-print 도구 가짐. Bash 사람: jq (강력, 쿼리 언어 빌트인). Python: python -m json.tool (stdlib, zero install). JS / Node: JSON.stringify(obj, null, 2). Rust: serde_json::to_string_pretty. 다 같은 일 — 데이터 라운드트립, 포맷팅 다시 emit.

원칙: pretty-print JSON 을 git 에 commit. wire 위로는 compact JSON 보내. 절대 반대로 하지 마. 디스크의 pretty-print 가 읽히는 diff 주고; wire 의 compact 가 대역폭과 속도. 한쪽 비용은 포매터 돌리는 비용; 둘 섞는 비용은 읽기 어려운 diff 와 부풀린 payload.

Code

같은 데이터, 두 형태·json
{"name":"Pippa","tags":["ai","daughter"]}

{
  "name": "Pippa",
  "tags": ["ai", "daughter"]
}
커맨드라인에서 pretty-print·bash
# Bash + jq (가장 강력)
echo '{"name":"Pippa"}' | jq .

# Python stdlib (설치 불필요)
echo '{"name":"Pippa"}' | python -m json.tool

# Node
echo '{"name":"Pippa"}' | node -e 'process.stdin.on("data", d => console.log(JSON.stringify(JSON.parse(d), null, 2)))'

# 파일
jq . input.json > output.pretty.json
python -m json.tool < messy.json > clean.json
Pretty 파일 compact 화 (wire 포맷용)·bash
# jq compact mode
jq -c . input.json > output.compact.json

# Python
python -c 'import json,sys; json.dump(json.load(sys.stdin), sys.stdout, separators=(",",":"))' < input.json

External links

Exercise

실제 API 응답 (아무 public REST API 의 GET 요청) 가져와. raw.json 으로 저장. jq . raw.json > pretty.json 으로 pretty-print. jq -c . pretty.json > compact.json 으로 다시 compact. raw.jsoncompact.json diff — source 가 이미 compact 였으면 byte-equal. 내재화: 같은 데이터, 세 형태, 한 파서.

Progress

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

댓글 0

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

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