Json 如何使用jq选择具有特定值的字段
我想选择字段结果具有特定值的父值 以下是我的json:Json 如何使用jq选择具有特定值的字段,json,select,jq,Json,Select,Jq,我想选择字段结果具有特定值的父值 以下是我的json: { "lde-poc-01": { "file_|-linux-debian-login-uidmin_|-/etc/login.defs_|-replace": { "name": "/etc/login.defs", "changes": {}, "pchanges": {}, "result": true,
{
"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"
}
}
}
我想获取comment/id的值和父值,如file_124;-mysoc-po-python-rm2_124;-/var/tmp/mysoc-po_124;-缺席,字段结果为false
我试图使用选择功能,但没有成功
感谢您的帮助您可以使用以下功能:
.[]
| to_entries
| map(select(.value.result == false))
| map({ name:.key, id:.value.__id__, comment:.value.comment})
mapselectcriteria用于放弃数组中不满足条件的项或项的字段
to|u条目用于从项的字段中创建一个键/值数组。类似于。[]|to|u条目|映射{name:.key,id:.value.。| id |,comment:.value.comment}我猜..[]| mapselect.result==false |映射{name:.key,id:.value.|我想。[]| to u条目| select.value.result==false |映射}{name:.key,id:.value.{uu id},comment:.value.comment}应该是正确的。使用map首先会丢失像file{u124;-linux debian..这样的关键数据,result会将name值获取为0,1,…我尝试了,但它返回了一个空数组。``jq'.[]to_条目{mapselect.result==false{map{name:.key,id:.value....comment:.comment}test json[]```好的,我可以用这个命令获得正确的值:jq.[]| to|u entries | mapselect.value.result==false | map{name:.key,id:.value。| id |,comment:.value.comment,}test.jsonSorry你说得对,我一开始有一个有效的解决方案,但我试图改进它,没有注意到名称更改为错误的值。我已经用你正在使用的有效解决方案更新了我的答案