使用该JSON通过卷曲使用JQ抓住特定的关键:从第n个对象值对的JSON阵列
{
"_class" : "org.jenkinsci.plugins.workflow.job.WorkflowRun",
"actions" : [
{
"_class" : "hudson.model.CauseAction",
"causes" : [
{
"_class" : "jenkins.branch.BranchIndexingCause",
"shortDescription" : "Branch indexing"
}
]
},
{
"_class" : "hudson.model.ParametersAction",
"parameters" : [ "..." ]
},
{
"_class" : "hudson.tasks.junit.TestResultAction",
"failCount" : 1,
"skipCount" : 14,
"totalCount" : 222,
"urlName" : "testReport"
}
],
"artifacts" : [ "..." ],
"result" : "UNSTABLE",
"previousBuild" : {
"number" : 98,
"url" : "<some Url>"
}
}
从詹金斯建立API调用采取为什么我能做到jq '{result}' <fileNameWithJSON>
并获得
{ "result" : "UNSTABLE" }
但我不能做jq '{.actions[2] failCount}' <fileNameWithJSON>
或其他变化,如
jq '{actions[2].failCount}'
jq '{actions[2] failCount}'
jq '{actions .[2].failCount}'
等
得到
{ "failCount" : "1" }
?
我要抢result
,以及actions[2] failCount
,actions[2] skipCount
和actions[2] totalCount
创建一个新的JSON这样的:
{ "result" : "UNSTABLE","failCount" : 1, "skipCount" : 14,"totalCount" : 222}
编辑:
我的目标是不必须重新指定密钥,以防其在api中更改。我基本上不想这样:
{result, "failCount":.actions[2].failCount, "skipCount":.actions[2].skipCount, "totalCount": .actions[2].totalCount}
这实质上是我正在寻找的最接近的东西。我不希望再次指定键,如下所示:'{result,“failCount”:.actions [2] .failCount,“skipCount”:.actions [2] .skipCount,“totalCount”: .actions [2] .totalCount}'。我想从api本身拉出来,所以如果api由于某种原因而改变,我不需要做出关键的改变。谢谢。 – Wimateeka