JQ 选择存在内键的对象

Rad*_*ord 5 json object filter jq

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"
         }
      }
   ]
}
Run Code Online (Sandbox Code Playgroud)

当我使用时cat my_bindings_test2.json | jq '.resources[] | .entity.credentials'我得到:

{}
{
  "hostname": "w",
  "port": 3306
}
{
  "credhub-ref": "ref3"
}
{
  "credhub-ref": "ref4"
}
Run Code Online (Sandbox Code Playgroud)

使用JQ,我如何得到以下结果?

{
  "credhub-ref": "ref3"
}
{
  "credhub-ref": "ref4"
}
Run Code Online (Sandbox Code Playgroud)

hek*_*mgl 7

像这样:

jq '.resources[].entity.credentials|select(has("credhub-ref"))' file.json
Run Code Online (Sandbox Code Playgroud)


ogu*_*ail 1

如果保证不会credhub-refnullor false,您可以使用select(.["credhub-ref"])

jq '.resources[].entity.credentials | select(.["credhub-ref"])' file
Run Code Online (Sandbox Code Playgroud)

否则请参阅@hek2mgl 的答案。