使用JQ选择存在内部键的对象

5

我正在尝试从以下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"
}
2个回答

7

就像这样:

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

1
如果可以保证credhub-ref不会为nullfalse,则可以使用select(.["credhub-ref"])
jq '.resources[].entity.credentials | select(.["credhub-ref"])' file

否则请参考@hek2mgl的答案。

1
谢谢。我是jq的新手,今天几次都差点成功了。我正在使用jq '.resources[].entity.credentials | select(.credhub-ref)' my_bindings_test.json - Radford
欢迎。当keyname包含模糊字符时,您可以使用.["keyname"]符号。 - oguz ismail
1
注意:如果“credhub-ref”为 nullfalse,则不会生成输出。不确定是否需要这样做。根据问题的实际情况,正确的答案是使用 has("credhub-ref") - 就像我展示的那样 ;) - hek2mgl
不要忘记 false - hek2mgl
1
不错!PS:nullfalse似乎是jq中唯一的“假值”,与JavaScript相反。这是个好消息!以前从来没有确定过。 - hek2mgl
1
再次感谢。该值永远不应为false或null,但我很高兴您提到了这一点,因为我将要将其推广以供未来使用。 - Radford

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接