Watch at Ease세상을 읽고, 일상의 답을 찾다.

기술 · 실용 가이드

jq의 cannot index string with string 오류? filter를 바꾸기 전에 JSON shape를 증명하세요

jq input mode와 모든 path의 type을 보존하고 증명된 mixed boundary만 guard하며 reject를 유지합니다.

ko jq JSON path type diagram
오리지널 편집 일러스트: prove the value type before indexing. Watch at Ease

jq object key lookup은 string, array, number 또는 boolean을 받으면 실패합니다. generator가 먼저 valid value를 출력하고 뒤의 다른 type에서 실패해 partial output과 nonzero exit가 함께 남을 수 있습니다.

raw bytes, input mode와 정확한 filter를 보존해 path-type ledger를 만듭니다. schema drift는 실패시키고 합법적인 mixed collection만 좁게 guard합니다.

bytes, mode, filter 고정

input file, jq --version, 전체 filter, shell quoting, upstream command와 -R, -s, -n을 보존합니다. -R은 string, -s는 array를 만듭니다.

보존 file이 JSON인지 확인하고 성공 record와 실패 record를 나란히 둡니다. 값 출력 후 exit 5도 실패입니다.

path-type ledger 작성

root와 각 traversal step에서 type을 실행합니다. RFC 8259는 object와 array를 구분하고 array elements가 다른 type일 수 있다고 합니다.

path, observed type과 count를 기록합니다. object의 .[]는 모든 member value를 내므로 다음 key가 string에 적용될 수 있습니다.

세 가지 shape failure 분리

wrong root traversal, mixed collection과 stringified object를 구분합니다. 마지막은 재해석 전에 provenance 결정을 해야 합니다.

shell echo를 authoritative bytes로 쓰지 않습니다. quoting이 내용을 바꿀 수 있어 저장 file을 직접 producer schema와 비교합니다.

증명된 boundary만 guard

mixed collection이 계약이면 type==object만 선택해 key를 읽고 나머지는 path와 type이 있는 reject ledger에 둡니다.

넓은 try, catch, optional은 empty로 error를 숨깁니다. 계약이 허용하고 reject count가 측정될 때만 사용합니다.

output, reject, exit 증명

object, array, string, boolean, null, missing key와 mixed array를 시험하고 accepted와 rejected를 input count와 맞춥니다.

완료 조건은 mode, path types, output count, reject 설명과 필수 object가 scalar일 때 nonzero failure입니다.

ko jq narrow guard and reject ledger diagram
오리지널 편집 일러스트: guard narrowly and keep rejected records visible. Watch at Ease