jq command

Younghwan Cha·2022년 12월 27일
0

linux command

목록 보기
8/16
post-thumbnail

cosmos 에서 persistent_peers 설정 할 때
github 에 올라간 json 파일에서 정보를 가져와 config.toml 파일에 넣어야하는 경우가 발생했다.
이때, jq 를 사용하여 처리하면 쉽게 처리할 수 있다.

... | jq -r '[foreach .peers.seeds[] as $item (INIT; UPDATE; EXTRACT)]'

curl -s https://raw.githubusercontent.com/cosmos/chain-registry/master/cosmoshub/chain.json | jq -r '[foreach .peers.seeds[] as $item (""; "\($item.id)@\($item.address)")] | join(",")'

https://hub.cosmos.network/main/getting-started/quickstart.html


요청의 반환값으로 아래와 같은 배열이 온 경우를 생각해보자.

[
	{
    	first:
        second:
    },
    {
    	first:
        second:
    }
]

위 배열의 첫번째 element 를 가져오려면 어떻게 해야할까?
jq 명령어를 사용하면 쉽게 구현 할 수 있다.

curl ... | jq '.[0]'

그렇다면, first 의 value 를 가져오려면..?
물론 가능하다!

curl ... | jq '.[0]' | jq .first

이런건 배울때마다 신기하고 재밌는 것 같다.
다른 방법도 많이 있을 것 같은데 배울때마다 정리해보도록 하자.


[ref]
https://stedolan.github.io/jq/manual/

profile
개발 기록

0개의 댓글