【问题标题】:JQ Select Objects Where Inner Key ExistsJQ选择存在内键的对象
【发布时间】:2019-08-10 14:33:03
【问题描述】:

我仅在以下 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"
}

【问题讨论】:

    标签: json object filter jq


    【解决方案1】:

    像这样:

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

    【讨论】:

      【解决方案2】:

      如果保证credhub-ref不会是nullfalse,则可以使用select(.["credhub-ref"])

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

      否则参考@hek2mgl 的回答。

      【讨论】:

      • 谢谢。 jq 的新手,我今天真的很接近好几次。我正在使用jq '.resources[].entity.credentials | select(.credhub-ref)' my_bindings_test.json
      • 欢迎。当键名包含不明确的字符时,您可以使用 .["keyname"] 表示法
      • 注意:如果 "credhub-ref" 为 nullfalse,则不会产生输出。不确定是否需要。正如已经提出的那样,该问题的正确答案是使用has("credhub-ref") - 就像我展示的那样;)
      • np。做得好! PS:nullfalse 似乎是 jq 中唯一的“虚假”值 - 与 javascript 相反。那是好消息!一直不确定
      • 再次感谢。该值不应为 false 或 null,但我很高兴您提到了这一点,因为我打算将其概括为将来使用。
      猜你喜欢
      • 1970-01-01
      • 2015-06-13
      • 1970-01-01
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-12
      相关资源
      最近更新 更多