JQ选择基于子对象的外部对象和子对象

时间:2019-03-26 01:09:16

标签: json object filter parent-child jq

仅当密钥credhub-ref存在于JSON之后的凭证对象中时,我才尝试选择凭证对象和特定的父级对象:

{
   "total_results": 23,
   "total_pages": 1,
   "prev_url": null,
   "next_url": null,
   "resources": [
      {
         "metadata": {
            "guid": "cd",
            "url": "/v2/service_bindings/cd",
            "created_at": "2019-03-04T21:18:53Z",
            "updated_at": "2019-03-04T21:18:53Z"
         },
         "entity": {
            "app_guid": "9c",
            "service_instance_guid": "66",
            "credentials": {
               "credhub-ref": "/c/54"
            },
            "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-04T21:18:53Z",
               "created_at": "2019-03-04T21:18:53Z"
            },
            "app_url": "/v2/apps/9c",
            "service_instance_url": "/v2/service_instances/66",
            "service_binding_parameters_url": "/v2/service_bindings/cd"
         }
      },
      {
         "metadata": {
            "guid": "cd",
            "url": "/v2/service_bindings/cd",
            "created_at": "2019-03-11T08:49:22Z",
            "updated_at": "2019-03-11T08:49:22Z"
         },
         "entity": {
            "app_guid": "ea",
            "service_instance_guid": "86",
            "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": "2019-03-11T08:49:22Z",
               "created_at": "2019-03-11T08:49:22Z"
            },
            "app_url": "/v2/apps/ea",
            "service_instance_url": "/v2/service_instances/86",
            "service_binding_parameters_url": "/v2/service_bindings/cd"
         }
      },
      {
         "metadata": {
            "guid": "e0",
            "url": "/v2/service_bindings/e0",
            "created_at": "2019-03-19T20:07:25Z",
            "updated_at": "2019-03-19T20:07:25Z"
         },
         "entity": {
            "app_guid": "73",
            "service_instance_guid": "52",
            "credentials": {
               "hostname": "10.13.7.64",
               "port": 3306,
               "name": "cf_52",
               "username": "w",
               "password": "w",
               "uri": "mysql://",
               "jdbcUrl": "jdbc:mysql://10.193.78.64:"
            },
            "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:25Z",
               "created_at": "2019-03-19T20:07:25Z"
            },
            "app_url": "/v2/apps/73",
            "service_instance_url": "/v2/service_instances/52",
            "service_binding_parameters_url": "/v2/service_bindings/e0"
         }
      },
      {
         "metadata": {
            "guid": "18",
            "url": "/v2/service_bindings/18",
            "created_at": "2019-03-19T20:07:27Z",
            "updated_at": "2019-03-19T20:07:27Z"
         },
         "entity": {
            "app_guid": "73",
            "service_instance_guid": "ae",
            "credentials": {
               "credhub-ref": "/c/54"
            },
            "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/apps/73",
            "service_instance_url": "/v2/service_instances/aec",
            "service_binding_parameters_url": "/v2/service_bindings/18"
         }
      }
   ]
}

当前查询仅返回凭据对象:

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

结果:

{
  "credhub-ref": "/c/42"
}
{
  "credhub-ref": "/c/54"
}

我需要一个查询,它也输出父对象app_guidservice_instance_guid,如下所示:

}
   "app_guid": "9c",
   "service_instance_guid": "66",
   "credentials": {
      "credhub-ref": "/c/54"
},
{
   "app_guid": "73",
   "service_instance_guid": "ae",
   "credentials": {
      "credhub-ref": "/c/54"
}

几个很好的例子可以帮助我更好地理解JQ的工作原理。

2 个答案:

答案 0 :(得分:1)

这是获得预期输出的多种方法之一:

jq '.resources[].entity | select(.credentials."credhub-ref") | {app_guid, service_instance_guid, credentials}' test.json

它将在每个.credentials.credhub-ref对象中搜索entity,并返回app_guid, service_instance_guid, credentials作为对象。

输出:

{
  "app_guid": "9c",
  "service_instance_guid": "66",
  "credentials": {
    "credhub-ref": "/c/54"
  }
}
{
  "app_guid": "73",
  "service_instance_guid": "ae",
  "credentials": {
    "credhub-ref": "/c/54"
  }
}

答案 1 :(得分:0)

或者,您的请求可以通过 jtc Unix实用程序来实现:

bash $ <file.json jtc -x'<credentials>l:[credhub-ref][-2]' -y[credentials] -y[app_guid] -y[service_instance_guid] -lj
[
   {
      "app_guid": "9c",
      "credentials": {
         "credhub-ref": "/c/54"
      },
      "service_instance_guid": "66"
   },
   {
      "app_guid": "73",
      "credentials": {
         "credhub-ref": "/c/54"
      },
      "service_instance_guid": "ae"
   }
]
bash $ 

常见(共享)步行路径细分(-x):

  • <credentials>l:-查找标签为credentials的所有JSON条目
  • [credhub-ref]在每个找到的条目选择记录credhub-ref中(如果该条目不存在,那么该搜索实例将被中止并再次进行迭代)
  • [-2]-从上次成功选择的条目开始,在JSON树中提升2个父级

-将选择资源中的每条记录。

然后,对于每个指定的单个路径(-y),步行都会在所选条目之外发生,从而为您提供所需的结果

选项-lj仅确保对结果步行进行正确的分组