jq를 사용하여 내부 배열의 값을 기반으로 객체 배열을 필터링하는 방법은 무엇입니까?
이 입력이 주어지면 :
[
{
"Id": "cb94e7a42732b598ad18a8f27454a886c1aa8bbba6167646d8f064cd86191e2b",
"Names": [
"condescending_jones",
"loving_hoover"
]
},
{
"Id": "186db739b7509eb0114a09e14bcd16bf637019860d23c4fc20e98cbe068b55aa",
"Names": [
"foo_data"
]
},
{
"Id": "a4b7e6f5752d8dcb906a5901f7ab82e403b9dff4eaaeebea767a04bac4aada19",
"Names": [
"jovial_wozniak"
]
},
{
"Id": "76b71c496556912012c20dc3cbd37a54a1f05bffad3d5e92466900a003fbb623",
"Names": [
"bar_data"
]
}
]
내부 배열 에 "데이터"를 포함 하지 않는 s를 가진 모든 객체를 반환 하는 jq 로 필터를 구성하려고 합니다. 출력은 줄 바꿈으로 구분됩니다. 위 데이터의 경우 원하는 출력은Id
Names
cb94e7a42732b598ad18a8f27454a886c1aa8bbba6167646d8f064cd86191e2b
a4b7e6f5752d8dcb906a5901f7ab82e403b9dff4eaaeebea767a04bac4aada19
나는 이것과 다소 가깝다고 생각합니다.
(. - select(.Names[] contains("data"))) | .[] .Id
그러나 select
필터가 올바르지 않고 컴파일되지 않습니다 (get error: syntax error, unexpected IDENT
).
아주 근접한! 당신에 select
표현, 당신은 파이프 (사용해야 |
하기 전에) contains
.
이 필터는 예상 출력을 생성합니다.
. - map(select(.Names[] | contains ("data"))) | .[] .Id
JQ 해설서는 구문의 일례가있다.
키의 내용을 기준으로 객체 필터링
E.g., I only want objects whose genre key contains "house".
$ json='[{"genre":"deep house"}, {"genre": "progressive house"}, {"genre": "dubstep"}]' $ echo "$json" | jq -c '.[] | select(.genre | contains("house"))' {"genre":"deep house"} {"genre":"progressive house"}
Colin D asks how to preserve the JSON structure of the array, so that the final output is a single JSON array rather than a stream of JSON objects.
The simplest way is to wrap the whole expression in an array constructor:
$ echo "$json" | jq -c '[ .[] | select( .genre | contains("house")) ]'
[{"genre":"deep house"},{"genre":"progressive house"}]
You can also use the map function:
$ echo "$json" | jq -c 'map(select(.genre | contains("house")))'
[{"genre":"deep house"},{"genre":"progressive house"}]
map unpacks the input array, applies the filter to every element, and creates a new array. In other words, map(f)
is equivalent to [.[]|f]
.
Here is another solution which uses any/2
map(select(any(.Names[]; contains("data"))|not)|.Id)[]
with the sample data and the -r
option it produces
cb94e7a42732b598ad18a8f27454a886c1aa8bbba6167646d8f064cd86191e2b
a4b7e6f5752d8dcb906a5901f7ab82e403b9dff4eaaeebea767a04bac4aada19
'Programing' 카테고리의 다른 글
수업 종류 확인 (0) | 2020.05.10 |
---|---|
푸시 알림 기술은 Android에서 어떻게 작동합니까? (0) | 2020.05.10 |
JWT를위한 최고의 HTTP 인증 헤더 유형 (0) | 2020.05.10 |
ng-click에서 원본 요소 가져 오기 (0) | 2020.05.10 |
웹 사이트에서 IE9가 호환 모드로 전환되는 이유는 무엇입니까? (0) | 2020.05.10 |