我的输入如下所示。我想搜索SearchString
密钥(您可以看到我们不能为它使用固定索引),当密钥出现时,从 base64 解密其值(可能使用@base64d
过滤器)。JQ可以做到这一点吗?如果是这样,如何?
[
{
"Name": "searchblock",
"Priority": 3,
"Statement": {
"RateBasedStatement": {
"Limit": 100,
"AggregateKeyType": "IP",
"ScopeDownStatement": {
"ByteMatchStatement": {
"SearchString": "Y2F0YWxvZ3NlYXJjaA==",
"FieldToMatch": {
"UriPath": {}
},
"TextTransformations": [
{
"Priority": 0,
"Type": "LOWERCASE"
}
],
"PositionalConstraint": "CONTAINS"
}
}
}
},
"Action": {
"Block": {}
},
"VisibilityConfig": {
"SampledRequestsEnabled": true,
"CloudWatchMetricsEnabled": true,
"MetricName": "searchblock"
}
},
{
"Name": "bot-block",
"Priority": 4,
"Statement": {
"ByteMatchStatement": {
"SearchString": "Ym90",
"FieldToMatch": {
"SingleHeader": {
"Name": "user-agent"
}
},
"TextTransformations": [
{
"Priority": 0,
"Type": "LOWERCASE"
}
],
"PositionalConstraint": "CONTAINS"
}
},
"Action": {
"Allow": {}
},
"VisibilityConfig": {
"SampledRequestsEnabled": true,
"CloudWatchMetricsEnabled": true,
"MetricName": "user-agent"
}
}
]
当固定路径不可用时,我们使用path
、paths
、getpath
和setpath
内置来执行此类操作。
getpath(paths | select(.[-1] == "SearchString")) |= @base64d
在线演示
walk
对于这种任务非常直观:
walk(if type == "object" and .SearchString
then .SearchString |= @base64d else . end)
使用这种方法,修改程序以使其更健壮也是微不足道的,例如检查.搜索字符串是一个字符串:
walk(if type == "object" and (.SearchString|type) == "string"
then .SearchString |= @base64d else . end)
注意:如果您的 jq 不包含walk
,您可以简单地从任何信誉良好的网站或 https://github.com/stedolan/jq/blob/master/src/builtin.jq 复制其def