我想删除所有与正则表达式匹配的字段,同时保留所有其他字段和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
Suggestions? If possible, I'd rather avoid with_entries
, but if that's the only way, then so be it.
Here's a simple solution using
walk
: