JQ Выбрать объекты, где существует внутренний ключ - PullRequest
1 голос
/ 20 марта 2019

Я пытаюсь выбрать учетные объекты, только если в них существует ключ 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"
}

Ответы [ 3 ]

1 голос
/ 20 марта 2019

Как это:

jq '.resources[].entity.credentials|select(has("credhub-ref"))' file.json
1 голос
/ 20 марта 2019

Если гарантируется, что credhub-ref не будет null или false, вы можете использовать select(.["credhub-ref"]):

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

В противном случае обратитесь к ответу @ hek2mgl.

1 голос
/ 20 марта 2019

в качестве альтернативы, вы можете рассмотреть возможность использования простой утилиты unix jtc для обхода JSON:

bash $ <file.json jtc -w'<credhub-ref>l:[-1]'
{
   "credhub-ref": "ref3"
}
{
   "credhub-ref": "ref4"
}
bash $ 
...