示例Json
{
"product": [
{
"versions": "1.0",
"components": [
{
"name": "JVM",
"version": "11.0.16"
},
{
"name": "TOMCAT",
"version": "9.0.56"
}
]
},
{
"versions": "1.1",
"components": [
{
"name": "JVM",
"version": "11.0.16"
},
{
"name": "TOMCAT",
"version": "9.0.56"
}
]
}
]
}
命令
jq -r '.product[] | .versions, .components[] | "\(.name): \(.version)"' version_mapper.json
输出
jq: error (at version_mapper.json:29): Cannot index string with string "version"
错误命令
jq -r '.product[] | .wrongKey, .components[] | "\(.name): \(.version)"' version_mapper.json
输出
null: null
JVM: 11.0.16
TOMCAT: 9.0.56
null: null
JVM: 11.0.16
TOMCAT: 9.0.56
不知道发生了什么
1条答案
按热度按时间llew8vvj1#
如果你想要每个组件
name
和version
,就不需要.versions
,这就足够了:输出
如果要包含产品本身的
versions
,可以在显示.versions
后循环遍历组件,例如以下筛选器输出
如果你不需要字符串插值,
"\(.name): \(.version)"
可以替换为join(": ")
,例如: