我有一个数组“ operations”,我想从中返回所有包含诸如"w51"
之类的匹配字符串的元素。到目前为止,我发现的所有样本都涉及键值对。我正在使用jq '.operations[]' < file
来检索元素。
{
"operations": [
[
"create",
"w51",
"rwt.widgets.Label",
{
"parent": "w41",
"style": [
"NONE"
],
"bounds": [
101,
0,
49,
42
],
"tabIndex": -1,
"customVariant": "variant_pufferLabelLogout"
}
],
[
"create",
"w39",
"rwt.widgets.Composite",
{
"parent": "w34",
"style": [
"NONE"
],
"children": [
"w52"
],
"bounds": [
0,
42,
762,
868
],
"tabIndex": -1,
"clientArea": [
0,
0,
762,
868
]
}
]
]
}
搜索包含“ w51”的数组元素时,我的预期输出是:
[
"create",
"w51",
"rwt.widgets.Label",
{
"parent": "w41",
"style": [
"NONE"
],
"bounds": [
101,
0,
49,
42
],
"tabIndex": -1,
"customVariant": "variant_pufferLabelLogout"
}
]
答案 0 :(得分:1)
如果您使用的是1.4版或更高版本的jq,则以下内容将产生所需的输出:
.operations[]
| select( index("w51") )
有许多选择,具体取决于您使用的jq版本。如果您的jq具有any/0
,则以下是一种有效的选择:
.operations[] | select( any(. == "w51" ) )