gpt4 book ai didi

json - JQ 选择存在内键的对象

转载 作者:行者123 更新时间:2023-12-05 01:41:01 25 4
gpt4 key购买 nike

仅当 key credhub-ref 存在于以下 JSON 中时,我才尝试选择凭证对象:

{
"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(has("credhub-ref"))' file.json

关于json - JQ 选择存在内键的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55251603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com