JQ筛选器,用于带有日期评估的父ID和子ID



我的最后期限很紧,正在为一个不公开SQL视图但通过API公开某些元素的产品进行变通。希望在卷曲查询之后在PIPE中进行此处理。

复制:

curl -x GET -u {credentials} {endpoint fqdn} | jq
{
"total_count": 34,
"Passwords": [
{
"username": "testusername",
"category": null,
"device_ids": [],
"view_users": "",
"view_groups": "",
"last_pw_change": "2021-04-13T13:30:03Z",
"notes": "",
"storage": "Normal",
"use_only_users": "",
"label": "EXPIRED",
"view_edit_groups": "",
"first_added": "2021-04-13T13:30:03.790Z",
"use_only_groups": "",
"storage_id": 1,
"view_edit_users": "admin",
"password": "",
"id": 36,
"custom_fields": [
{
"notes": null,
"key": "expiry-event",
"value": "2021-4-13"
}
]
}
],
}

上面是一个通过curl返回的示例。我希望能够获取用户名、STORAGE_ID,然后获取CUSTOM_FIELDS>KEY和CUSTOM_FIELDS>值

当密钥=到期事件并且该密钥的VALUE在今天的日期之前。

JQ过滤可以做到这一点吗?我不确定日期选项是否是JQ中的一个函数。但如果能提供任何帮助,尽可能靠近我们,我们将不胜感激。

我尝试过过滤到单独的数组中,但我得到了一个FAILEDTOBUILDBODY错误。谢谢

给定您的示例输入(没有使其在技术上作为JSON无效的额外逗号(,以下内容将生成如下所示的输出:

def prior($yymmdd): sub("-(?<d>\d)-"; "-0(.d)-") <= ($yymmdd);
(now|strflocaltime("%Y-%m-%d")) as $now
| .Passwords[]
| {username,
storage,
custom_fields : .custom_fields | map(select(.key == "expiry-event" and (.value|prior($now)))) }

您可以很容易地删除任何不需要的字段,例如使用del/1,或通过指定所需的键。

输出

{
"username": "testusername",
"storage": "Normal",
"custom_fields": [
{
"notes": null,
"key": "expiry-event",
"value": "2021-4-13"
}
]
}

相关内容

最新更新