如何使用jq生成记录的单行输出

时间:2018-04-11 15:56:43

标签: jq

我有以下JSON


{
   "guid": "dce38b4b-9989-42d2-b6bd-702645e344cf",
   "name": "dev",
   "apps": [
      {
         "guid": "5d5498cb-a885-4242-a55a-d7d286a1cf48",
         "urls": [
            "nodered.cloudfoundry.org"
         ],
         "routes": [
            {
               "guid": "aca4d04b-f99d-4b43-afaa-82ab41afa07c",
               "host": "nodered-test",
               "port": null,
               "path": "",
               "domain": {
                  "guid": "f4b90d7e-2cd3-4d30-b200-f28bbaf6be20",
                  "name": "cloudfoundry.org"
               }
            }
         ],
         "service_count": 1,
         "service_names": [
            "nodered-test-cloudantNoSQLDB"
         ],
         "running_instances": 1,
         "name": "perik-nodered-test",
         "production": false,
         "space_guid": "dce38b4b-9989-42d2-b6bd-702645e344cf",
         "stack_guid": "ac91d31a-86a3-453b-babf-8d49c9d763fc",
         "buildpack": null,
         "detected_buildpack": "SDK for Node.js(TM) (ibm-node.js-6.13.0, buildpack-v3.20-20180403-1426)",
         "detected_buildpack_guid": "33e9e82f-8846-4362-a60a-92964285a31e",
         "environment_json": {},
         "memory": 256,
         "instances": 1,
         "disk_quota": 1024,
         "state": "STARTED",
         "version": "8c8c97a0-bc2e-424c-a0a3-d64704feb634",
         "command": null,
         "console": false,
         "debug": null,
         "staging_task_id": "539f460c-e4d2-49f9-b5e5-9f4fd31a8370",
         "package_state": "STAGED",
         "health_check_type": "port",
         "health_check_timeout": null,
         "health_check_http_endpoint": null,
         "staging_failed_reason": null,
         "staging_failed_description": null,
         "diego": true,
         "docker_image": null,
         "package_updated_at": "2018-04-10T17:32:06Z",
         "detected_start_command": "./vendor/initial_startup.rb",
         "enable_ssh": true,
         "ports": null
      },
      {
         "guid": "4089ce19-19fb-467b-8876-3635819d5d91",
         "urls": [
            "nodered.cloudfoundry.org"
         ],
         "routes": [
            {
               "guid": "b79988d8-5ac5-455d-8fdd-00bc208dd2bc",
               "host": "NodeRedTestApp",
               "port": null,
               "path": "",
               "domain": {
                  "guid": "f4b90d7e-2cd3-4d30-b200-f28bbaf6be20",
                  "name": "cloudfoundry.org"
               }
            }
         ],
         "service_count": 1,
         "service_names": [
            "NodeRedTestApp-cloudantNoSQLDB"
         ],
         "running_instances": 1,
         "name": "perik-NodeRedTestApp",
         "production": false,
         "space_guid": "dce38b4b-9989-42d2-b6bd-702645e344cf",
         "stack_guid": "ac91d31a-86a3-453b-babf-8d49c9d763fc",
         "buildpack": null,
         "detected_buildpack": "SDK for Node.js(TM) (ibm-node.js-6.13.0, buildpack-v3.20-20180403-1426)",
         "detected_buildpack_guid": "33e9e82f-8846-4362-a60a-92964285a31e",
         "environment_json": {},
         "memory": 256,
         "instances": 1,
         "disk_quota": 1024,
         "state": "STARTED",
         "version": "17175bdb-df93-4745-9a17-cf214fe05976",
         "command": null,
         "console": false,
         "debug": null,
         "staging_task_id": "5c74633a-25db-4adf-9eb8-09454a70be52",
         "package_state": "STAGED",
         "health_check_type": "port",
         "health_check_timeout": null,
         "health_check_http_endpoint": null,
         "staging_failed_reason": null,
         "staging_failed_description": null,
         "diego": true,
         "docker_image": null,
         "package_updated_at": "2018-04-09T21:23:27Z",
         "detected_start_command": "./vendor/initial_startup.rb",
         "enable_ssh": true,
         "ports": null
      }
   ]

}

并使用

获取以下值

cat test.json | jq -r'.apps []。routes []。host,.apps [] .state,.apps [] .package_updated_at',它产生以下输出

nodered测试
NodeRedTestApp
已启动
已启动
2018-04-10T17:32:06Z
2018-04-09T21:23:27Z

如何将输出作为

nodered-test STARTED 2018-04-10T17:32:06Z
NodeRedTestApp开始2018-04-09T21:23:27Z

这样我就可以将它分配给bash脚本数组了。

1 个答案:

答案 0 :(得分:1)

=IF((AND(E50="Yes",E51="Yes",E52="Yes")), "Yes, the effect is material", IF(OR(E50="",E51="",E52=""), "", "No, the effect is not material")) 解决方案:

jq

输出:

jq -r '.apps[] | [.routes[].host, .state, .package_updated_at] | join(" ")' test.json