Search code examples
jsonselectjqcontains

how to select based on a substring in jq


With jq, how do I select only elements that contain the word "Star" in the following file movies:

$ cat movies
[
  {
    "title": "Dark Knight, The",
    "year": 2008
  },
  {
    "title": "Star Trek Beyond",
    "year": 2016
  },
  {
    "title": "Star Wars: Episode VII - The Force Awakens",
    "year": 2015
  }
]

Expected output:

{
  "title": "Star Trek Beyond",
  "year": 2016
}
{
  "title": "Star Wars: Episode VII - The Force Awakens",
  "year": 2015
}

Solution

  • jq '.[] | select(.title | contains("Star"))' movies