如何使用jq选择具有特定值的字段

时间:2019-06-25 09:50:00

标签: json select jq

我想选择父值,其中字段结果具有特定值

这是我拥有的json:

{
    "lde-poc-01": {
        "file_|-linux-debian-login-uidmin_|-/etc/login.defs_|-replace": {
            "name": "/etc/login.defs",
            "changes": {},
            "pchanges": {},
            "result": true,
            "comment": "No changes needed to be made",
            "__sls__": "dev/linux/ubuntu/login",
            "__run_num__": 0,
            "start_time": "14:40:20.708675",
            "duration": 17.447,
            "__id__": "linux-debian-login-uidmin"
        },
        "file_|-linux-debian-login-uidmax_|-/etc/login.defs_|-replace": {
            "name": "/etc/login.defs",
            "changes": {},
            "pchanges": {},
            "result": true,
            "comment": "No changes needed to be made",
            "__sls__": "dev/linux/ubuntu/login",
            "__run_num__": 1,
            "start_time": "14:40:20.726336",
            "duration": 10.614,
            "__id__": "linux-debian-login-uidmax"
        },
        "file_|-linux-debian-login-sysuidmax_|-/etc/login.defs_|-replace": {
            "name": "/etc/login.defs",
            "changes": {},
            "pchanges": {},
            "result": true,
            "comment": "No changes needed to be made",
            "__sls__": "dev/linux/ubuntu/login",
            "__run_num__": 2,
            "start_time": "14:40:20.737165",
            "duration": 10.515,
            "__id__": "linux-debian-login-sysuidmax"
        },
        "file_|-linux-debian-login-gidmin_|-/etc/login.defs_|-replace": {
            "name": "/etc/login.defs",
            "changes": {},
            "pchanges": {},
            "result": true,
            "comment": "No changes needed to be made",
            "__sls__": "dev/linux/ubuntu/login",
            "__run_num__": 3,
            "start_time": "14:40:20.747892",
            "duration": 10.201,
            "__id__": "linux-debian-login-gidmin"
        },
        "file_|-linux-ubuntu-docker_|-/etc/apt/sources.list.d/docker.list_|-managed": {
            "changes": {},
            "pchanges": {},
            "comment": "File /etc/apt/sources.list.d/docker.list is in the correct state",
            "name": "/etc/apt/sources.list.d/docker.list",
            "result": true,
            "__sls__": "dev/linux/ubuntu/apt",
            "__run_num__": 9,
            "start_time": "14:40:20.873513",
            "duration": 40.569,
            "__id__": "linux-ubuntu-docker"
        },
        "cmd_|-linux-ubuntu-unattended-updrade-service_|-systemctl daemon-reload_|-run": {
            "changes": {},
            "result": false,
            "duration": 0.008,
            "start_time": "14:40:21.159608",
            "comment": "State was not run because none of the onchanges reqs changed",
            "__state_ran__": false,
            "__run_num__": 20,
            "__sls__": "dev/linux/ubuntu/apt"
        },
        "file_|-linux-ubuntu-upgrade-script-telegraf_|-/etc/telegraf/check-update.sh_|-managed": {
            "changes": {},
            "pchanges": {},
            "comment": "File /etc/telegraf/check-update.sh is in the correct state",
            "name": "/etc/telegraf/check-update.sh",
            "result": true,
            "__sls__": "dev/linux/ubuntu/apt",
            "__run_num__": 21,
            "start_time": "14:40:21.159685",
            "duration": 42.313,
            "__id__": "linux-ubuntu-upgrade-script-telegraf"
        },
        "file_|-linux-ubuntu-upgrade-telegraf_|-/etc/telegraf/telegraf.d/update.conf_|-managed": {
            "changes": {},
            "pchanges": {},
            "comment": "File /etc/telegraf/telegraf.d/update.conf is in the correct state",
            "name": "/etc/telegraf/telegraf.d/update.conf",
            "result": true,
            "__sls__": "dev/linux/ubuntu/apt",
            "__run_num__": 22,
            "start_time": "14:40:21.202254",
            "duration": 51.238,
            "__id__": "linux-ubuntu-upgrade-telegraf"
        },
        "file_|-mysoc-po-python-rm2_|-/var/tmp/mysoc-po_|-absent": {
            "name": "/var/tmp/mysoc-po",
            "changes": {},
            "pchanges": {},
            "result": false,
            "comment": "File /var/tmp/mysoc-po is not present",
            "__sls__": "dev/mysoc-po",
            "__run_num__": 236,
            "start_time": "14:40:38.161788",
            "duration": 0.305,
            "__id__": "mysoc-po-python-rm2"
        }
    }
}

我想获取注释/ id的值和父值,例如(“ file_ | -mysoc-po-python-rm2_ |-/ var / tmp / mysoc-po_ | -absent”),其中字段结果为错误。

我尝试使用选择功能,但没有成功

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您可以使用以下内容:

.[]
| to_entries
| map(select(.value.result == false))
| map({ name:.key, id:.value.__id__, comment:.value.comment})

map(select(criteria))用于丢弃不满足条件的数组项(在这种情况下为项的字段)。

to_entries用于从项目的字段中构建键/值的数组。