Radford
Radford

Reputation: 169

JQ Select Objects Where Inner Key Exists

I am trying to select credential objects only if key credhub-ref exist within them from the following 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"
         }
      }
   ]
}

When I use cat my_bindings_test2.json | jq '.resources[] | .entity.credentials' I get:

{}
{
  "hostname": "w",
  "port": 3306
}
{
  "credhub-ref": "ref3"
}
{
  "credhub-ref": "ref4"
}

Using JQ, how would I get the following result?

{
  "credhub-ref": "ref3"
}
{
  "credhub-ref": "ref4"
}

Upvotes: 5

Views: 6957

Answers (2)

hek2mgl
hek2mgl

Reputation: 158190

Like this:

jq '.resources[].entity.credentials|select(has("credhub-ref"))' file.json

Upvotes: 7

oguz ismail
oguz ismail

Reputation: 50805

If it is guaranteed that credhub-ref won't be null or false, you can use select(.["credhub-ref"]):

jq '.resources[].entity.credentials | select(.["credhub-ref"])' file

Otherwise refer to @hek2mgl's answer.

Upvotes: 1

Related Questions