【发布时间】:2020-05-17 23:01:59
【问题描述】:
我想删除与正则表达式匹配的所有字段,同时保留所有其他字段和 JSON 的结构。 例如,假设 JSON 如下所示。
{
"this": {
"foobar": {
"that": "too"
},
"baz": 3,
"foo": 1
"morefoo": {
"foosball": "hi"
}
}
}
那么期望的输出将是
{
"this": {
"baz": 3,
"morefoo": {}
}
}
可以通过明确命名此处讨论的字段来干净地做到这一点:
Delete objects and arrays with jq which match a key
jq 'del(.. | .foo?, .foobar?, .foosball?)'
但我想用正则表达式来做到这一点,比如
jq 'del(.. | .("^foo")?)' # This does not work
建议?如果可能的话,我宁愿避免with_entries,但如果这是唯一的方法,那就这样吧。
【问题讨论】: