2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

#jq コマンドで #JSON の ネストされた配列の中のオブジェクトの中の配列からちょっと複雑な検索をする ( select filter )

Last updated at Posted at 2019-04-12
  • Github Issue API で得られるようなリストを想定
  • JSON全体が配列である
  • 配列内の各要素の labels 配列が複数のオブジェクトを持っている
  • オブジェクトの name が特定の value を持っている要素だけを select したい
  • 元の配列、オブジェクト構造を崩さずに復元したい

JSON input ( eg.json )

[
  {
    "labels": [
      {
        "name": "feature"
      }
    ]
  },
  {
    "labels": [
      {
        "name": "bug"
      },
      {
        "name": "important"
      }
    ]
  },
  {
    "labels": [
      {
        "name": "fix"
      },
      {
        "name": "important"
      }
    ]
  }
]

filter pattern 1

$ cat tmp/eg.json | jq '[.[] | select(.labels[] | .name == "fix")]'
[
  {
    "labels": [
      {
        "name": "fix"
      },
      {
        "name": "important"
      }
    ]
  }
]

filter pattern 2

$ cat tmp/eg.json | jq '[.[] | select(.labels[] | .name == "important")]'
[
  {
    "labels": [
      {
        "name": "bug"
      },
      {
        "name": "important"
      }
    ]
  },
  {
    "labels": [
      {
        "name": "fix"
      },
      {
        "name": "important"
      }
    ]
  }
]

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?