简体   繁体   中英

How to filter jq based on the key value?

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
.releases[] | select(.name | index("release-1")) | .artifacts[].commitId

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

Result:

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

JqPlay demo

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

is another way.

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

is yet another way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM