Avery Magnotti Avery Magnotti - 6 months ago 9
Linux Question

Excluding unwanted names from json with jq

Suppose I curl a json file, like so:

curl https://api.github.com/repos/ruby/ruby/tags -o ruby.json


[
{
"name": "yarv_migration_base",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/yarv_migration_base",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/yarv_migration_base",
"commit": {
"sha": "20c8803697df39a086175a5eb2f8e8ee00bf1e2f",
"url": "https://api.github.com/repos/ruby/ruby/commits/20c8803697df39a086175a5eb2f8e8ee00bf1e2f"
}
},
{
"name": "v2_3_1",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_3_1",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_3_1",
"commit": {
"sha": "5827d8e887d881eb3a6e6ea7410590261c90545f",
"url": "https://api.github.com/repos/ruby/ruby/commits/5827d8e887d881eb3a6e6ea7410590261c90545f"
}
},
{
"name": "v2_3_0",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_3_0",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_3_0",
"commit": {
"sha": "a837be87fdf580ac4fd58c4cb2f1ee16bab11b99",
"url": "https://api.github.com/repos/ruby/ruby/commits/a837be87fdf580ac4fd58c4cb2f1ee16bab11b99"
}
},
{
"name": "v2_3_0_preview2",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_3_0_preview2",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_3_0_preview2",
"commit": {
"sha": "e3434401aca2e331132652d4458366267e8cf378",
"url": "https://api.github.com/repos/ruby/ruby/commits/e3434401aca2e331132652d4458366267e8cf378"
}
},
{
"name": "v2_3_0_preview1",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_3_0_preview1",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_3_0_preview1",
"commit": {
"sha": "9993701c7d3d83e24699177fef3238d8bf7bbbab",
"url": "https://api.github.com/repos/ruby/ruby/commits/9993701c7d3d83e24699177fef3238d8bf7bbbab"
}
},
{
"name": "v2_2_5",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_2_5",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_2_5",
"commit": {
"sha": "449169fd8cfe4253381c40f595097ed50932bdae",
"url": "https://api.github.com/repos/ruby/ruby/commits/449169fd8cfe4253381c40f595097ed50932bdae"
}
},
{
"name": "v2_2_4",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_2_4",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_2_4",
"commit": {
"sha": "9081c2c61ac9f7f9bdcbf054f33b2dc42740e85f",
"url": "https://api.github.com/repos/ruby/ruby/commits/9081c2c61ac9f7f9bdcbf054f33b2dc42740e85f"
}
},
{
"name": "v2_2_3",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_2_3",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_2_3",
"commit": {
"sha": "acaf2ee762b08a4ccb23fef46cd93da1616d4d32",
"url": "https://api.github.com/repos/ruby/ruby/commits/acaf2ee762b08a4ccb23fef46cd93da1616d4d32"
}
},
{
"name": "v2_2_2",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_2_2",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_2_2",
"commit": {
"sha": "a9721a259665149b1b9ff0beabcf5f8dc0136120",
"url": "https://api.github.com/repos/ruby/ruby/commits/a9721a259665149b1b9ff0beabcf5f8dc0136120"
}
},
{
"name": "v2_2_1",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_2_1",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_2_1",
"commit": {
"sha": "10bc9b85cba61af65dea858a2952ae04126a5d4d",
"url": "https://api.github.com/repos/ruby/ruby/commits/10bc9b85cba61af65dea858a2952ae04126a5d4d"
}
},
{
"name": "v2_2_0",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_2_0",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_2_0",
"commit": {
"sha": "7393bf6a5cfff63683f36535e293caaa0d4c5be0",
"url": "https://api.github.com/repos/ruby/ruby/commits/7393bf6a5cfff63683f36535e293caaa0d4c5be0"
}
},
{
"name": "v2_2_0_rc1",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_2_0_rc1",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_2_0_rc1",
"commit": {
"sha": "050b43acced48e2699f4c57df65e207da4f0aa5a",
"url": "https://api.github.com/repos/ruby/ruby/commits/050b43acced48e2699f4c57df65e207da4f0aa5a"
}
},
{
"name": "v2_2_0_preview2",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_2_0_preview2",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_2_0_preview2",
"commit": {
"sha": "2e968a23387c3255379a9bf91f3ecdafb01d940d",
"url": "https://api.github.com/repos/ruby/ruby/commits/2e968a23387c3255379a9bf91f3ecdafb01d940d"
}
},
{
"name": "v2_2_0_preview1",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_2_0_preview1",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_2_0_preview1",
"commit": {
"sha": "ac98aa3101ae3cf09c3bb318e22b72404150f106",
"url": "https://api.github.com/repos/ruby/ruby/commits/ac98aa3101ae3cf09c3bb318e22b72404150f106"
}
},
{
"name": "v2_1_10",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_1_10",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_1_10",
"commit": {
"sha": "410b031acb0b85e86564cc5d6fdde57b1251505f",
"url": "https://api.github.com/repos/ruby/ruby/commits/410b031acb0b85e86564cc5d6fdde57b1251505f"
}
},
{
"name": "v2_1_9",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_1_9",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_1_9",
"commit": {
"sha": "22b2eface036901d27bfd9a22057427e4cce5f06",
"url": "https://api.github.com/repos/ruby/ruby/commits/22b2eface036901d27bfd9a22057427e4cce5f06"
}
},
{
"name": "v2_1_8",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_1_8",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_1_8",
"commit": {
"sha": "4876b9a68c6d04717c35c01b071e88eaf09a0bf7",
"url": "https://api.github.com/repos/ruby/ruby/commits/4876b9a68c6d04717c35c01b071e88eaf09a0bf7"
}
},
{
"name": "v2_1_7",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_1_7",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_1_7",
"commit": {
"sha": "0f664738e9a1219767ab0d0d168902977305a61d",
"url": "https://api.github.com/repos/ruby/ruby/commits/0f664738e9a1219767ab0d0d168902977305a61d"
}
},
{
"name": "v2_1_6",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_1_6",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_1_6",
"commit": {
"sha": "fe8cc13685a847f5b4b687d9edb88f5bee58fd70",
"url": "https://api.github.com/repos/ruby/ruby/commits/fe8cc13685a847f5b4b687d9edb88f5bee58fd70"
}
},
{
"name": "v2_1_5",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_1_5",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_1_5",
"commit": {
"sha": "0d74082eced0254a30b8f09a4d65fff357fdc6cd",
"url": "https://api.github.com/repos/ruby/ruby/commits/0d74082eced0254a30b8f09a4d65fff357fdc6cd"
}
},
{
"name": "v2_1_4",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_1_4",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_1_4",
"commit": {
"sha": "e966a41ca690a82114719d9cae85c7f12c954ac6",
"url": "https://api.github.com/repos/ruby/ruby/commits/e966a41ca690a82114719d9cae85c7f12c954ac6"
}
},
{
"name": "v2_1_3",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_1_3",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_1_3",
"commit": {
"sha": "4cb2998f59939a8f275ea74079a4538f721a24ad",
"url": "https://api.github.com/repos/ruby/ruby/commits/4cb2998f59939a8f275ea74079a4538f721a24ad"
}
},
{
"name": "v2_1_2",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_1_2",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_1_2",
"commit": {
"sha": "e9dce8d1b482200685996f64cc2c3bd6ba790110",
"url": "https://api.github.com/repos/ruby/ruby/commits/e9dce8d1b482200685996f64cc2c3bd6ba790110"
}
},
{
"name": "v2_1_1",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_1_1",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_1_1",
"commit": {
"sha": "1980b4d4e4cc1dfd7f04d88c03e9f0a60dd4e94e",
"url": "https://api.github.com/repos/ruby/ruby/commits/1980b4d4e4cc1dfd7f04d88c03e9f0a60dd4e94e"
}
},
{
"name": "v2_1_0",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_1_0",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_1_0",
"commit": {
"sha": "3603063e4385c45db103d979311f689e9146383e",
"url": "https://api.github.com/repos/ruby/ruby/commits/3603063e4385c45db103d979311f689e9146383e"
}
},
{
"name": "v2_1_0_rc1",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_1_0_rc1",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_1_0_rc1",
"commit": {
"sha": "9881a183bde20d1c174d33d8a2f637e9c092a08b",
"url": "https://api.github.com/repos/ruby/ruby/commits/9881a183bde20d1c174d33d8a2f637e9c092a08b"
}
},
{
"name": "v2_1_0_preview2",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_1_0_preview2",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_1_0_preview2",
"commit": {
"sha": "834679d12c16cb142a8b534be137fc3e3a3c82a4",
"url": "https://api.github.com/repos/ruby/ruby/commits/834679d12c16cb142a8b534be137fc3e3a3c82a4"
}
},
{
"name": "v2_1_0_preview1",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_1_0_preview1",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_1_0_preview1",
"commit": {
"sha": "928c4a92de2df1955a8adf2f544c7cbec11b4711",
"url": "https://api.github.com/repos/ruby/ruby/commits/928c4a92de2df1955a8adf2f544c7cbec11b4711"
}
},
{
"name": "v2_0_0_648",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_0_0_648",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_0_0_648",
"commit": {
"sha": "03ec9ed5c93d6681bff894e566f9affe9d80a248",
"url": "https://api.github.com/repos/ruby/ruby/commits/03ec9ed5c93d6681bff894e566f9affe9d80a248"
}
},
{
"name": "v2_0_0_647",
"zipball_url": "https://api.github.com/repos/ruby/ruby/zipball/v2_0_0_647",
"tarball_url": "https://api.github.com/repos/ruby/ruby/tarball/v2_0_0_647",
"commit": {
"sha": "c60c15f371a6d2f2e024ebd75ee9b3fefa7d7118",
"url": "https://api.github.com/repos/ruby/ruby/commits/c60c15f371a6d2f2e024ebd75ee9b3fefa7d7118"
}
}
]


I can then run
cat ruby.json | jq '.[0] | .tarball_url'
and get the latest tarball for Ruby. However, I'd like to not fetch any tarballs that are previews of Ruby or contain something like
"yarv_migration_base"
. Is there a way to exclude data using jq?

Answer

Filter it out first, then get the first (assuming it's sorted with most recent first).

jq 'map(select(.name != "yarv_migration_base" and
        (.name | contains("preview") | not)
    ))[0].tarball_url' ruby.json
Comments