我正在尝试从以下JSON中仅选择具有credhub-ref
键的凭据对象:
{
"total_results": 23,
"total_pages": 1,
"prev_url": null,
"next_url": null,
"resources": [
{
"entity": {
"credentials": {},
"binding_options": {},
"gateway_data": null,
"gateway_name": "",
"syslog_drain_url": null,
"volume_mounts": [],
"name": null,
"last_operation": {
"type": "create",
"state": "succeeded",
"description": "",
"updated_at": "2018-10-15T19:13:57Z",
"created_at": "2018-10-15T19:13:57Z"
},
"app_url": "/v2/3"
}
},
{
"entity": {
"app_guid": "sd",
"service_instance_guid": "sd",
"credentials": {
"hostname": "w",
"port": 3306
},
"binding_options": {},
"gateway_data": null,
"gateway_name": "",
"syslog_drain_url": null,
"volume_mounts": [],
"name": null,
"last_operation": {
"type": "create",
"state": "succeeded",
"description": "",
"updated_at": "2018-10-15T19:24:06Z",
"created_at": "2018-10-15T19:24:06Z"
},
"app_url": "/v2/3"
}
},
{
"entity": {
"credentials": {
"credhub-ref": "ref3"
},
"binding_options": {},
"gateway_data": null,
"gateway_name": "",
"syslog_drain_url": null,
"volume_mounts": [],
"name": null,
"last_operation": {
"type": "create",
"state": "succeeded",
"description": "",
"updated_at": "2019-03-19T20:07:27Z",
"created_at": "2019-03-19T20:07:27Z"
},
"app_url": "/v2/45"
}
},
{
"entity": {
"credentials": {
"credhub-ref": "ref4"
},
"binding_options": {},
"gateway_data": null,
"gateway_name": "",
"syslog_drain_url": null,
"volume_mounts": [],
"name": null,
"last_operation": {
"type": "create",
"state": "succeeded",
"description": "",
"updated_at": "2019-03-19T20:07:27Z",
"created_at": "2019-03-19T20:07:27Z"
},
"app_url": "/v2/45"
}
}
]
}
当我使用 cat my_bindings_test2.json | jq '.resources[] | .entity.credentials'
命令时,返回结果如下:
{}
{
"hostname": "w",
"port": 3306
}
{
"credhub-ref": "ref3"
}
{
"credhub-ref": "ref4"
}
使用 JQ,我应该如何获得以下结果?
{
"credhub-ref": "ref3"
}
{
"credhub-ref": "ref4"
}
jq '.resources[].entity.credentials | select(.credhub-ref)' my_bindings_test.json
。 - Radford.["keyname"]
符号。 - oguz ismailnull
或false
,则不会生成输出。不确定是否需要这样做。根据问题的实际情况,正确的答案是使用has("credhub-ref")
- 就像我展示的那样 ;) - hek2mglfalse
。 - hek2mglnull
和false
似乎是jq中唯一的“假值”,与JavaScript相反。这是个好消息!以前从来没有确定过。 - hek2mgl