簡體   English   中英

如何根據鍵值過濾jq?

[英]How to filter jq based on the key value?

我對 JQ 庫很陌生,我想使用其名稱(例如 release-1)過濾 json 文件,然后我想返回與名稱相同的 object 中的所有 commitId 的鍵值。

我的 json 文件

{
   "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"
            }
         ]
      }
   ]
}

預計 Output

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

將顯示name包含 ( index() ) release-1每個commitId

結果:

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

JqPlay 演示

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

是另一種方式。

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

是另一種方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM