Home > Net >  How to filter jq based on the key value?
How to filter jq based on the key value?

Time:09-07

I am quite new with JQ library, I want to filter the json file using their name(eg. release-1),Then I want to return key value of the all commitId in the same object as the name.

My json file

{
   "releases":[
      {
         "name":[
            "release-1"
         ],
         "artifacts":[
            {
               "name":"pkg-1",
               "commitId":"523asdc3"
            },
            {
               "name":"pkg-2",
               "commitId":"523asdc3"
            },
            {
               "name":"pkg-3",
               "commitId":"523asdc3"
            }
         ]
      },
      {
         "name":[
            "release-2"
         ],
         "artifacts":[
            {
               "name":"pkg-3",
               "commitId":"523asdc3"
            },
            {
               "name":"pkg-4",
               "commitId":"523asdc3"
            },
            {
               "name":"pkg-5",
               "commitId":"523asdc3"
            }
         ]
      }
   ]
}

Expected Output

523asdc3
523asdc3
523asdc3

CodePudding user response:

.releases[] | select(.name | index("release-1")) | .artifacts[].commitId

Will show each commitId where name contains (index()) release-1.

Result:

"523asdc3"
"523asdc3"
"523asdc3"

JqPlay demo

  • Related