Programing

jq를 사용하여 내부 배열의 값을 기반으로 객체 배열을 필터링하는 방법은 무엇입니까?

lottogame 2020. 5. 10. 10:15
반응형

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 로 필터를 구성하려고 합니다. 출력은 줄 바꿈으로 구분됩니다. 위 데이터의 경우 원하는 출력은IdNames

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

참고URL : https://stackoverflow.com/questions/26701538/how-to-filter-an-array-of-objects-based-on-values-in-an-inner-array-with-jq

반응형