コンテンツにスキップ

jq

サンプルデータを元にした使用例

$ curl -s -L https://facebook.github.io/react-native/movies.json
{
  "title": "The Basics - Networking",
  "description": "Your app fetched this from a remote endpoint!",
  "movies": [
    { "id": "1", "title": "Star Wars", "releaseYear": "1977" },
    { "id": "2", "title": "Back to the Future", "releaseYear": "1985" },
    { "id": "3", "title": "The Matrix", "releaseYear": "1999" },
    { "id": "4", "title": "Inception", "releaseYear": "2010" },
    { "id": "5", "title": "Interstellar", "releaseYear": "2014" }
  ]
}

titleだけ表示( -r" を削除)

$ jq '.title' movies.json 
"The Basics - Networking"
$ jq -r '.title' movies.json 
The Basics - Networking

id=1のmovieだけ表示

$ jq -r '.movies[] | select(.id == "1")' movies.json 
{
  "id": "1",
  "title": "Star Wars",
  "releaseYear": "1977"
}

titleに"In"を含むものだけを表示

$ jq -r '.movies[] | select(.title|contains("In"))' movies.json 
{
  "id": "4",
  "title": "Inception",
  "releaseYear": "2010"
}
{
  "id": "5",
  "title": "Interstellar",
  "releaseYear": "2014"
}

これをJSONとして配列出力する場合は前後に [] を追加する。

$ jq -r '[.movies[] | select(.title|contains("In"))]' movies.json 
[
  {
    "id": "4",
    "title": "Inception",
    "releaseYear": "2010"
  },
  {
    "id": "5",
    "title": "Interstellar",
    "releaseYear": "2014"
  }
]

最終更新日: 2021-05-17 02:51:56