使用休息从马拉松任务列表中获取任务 API
Get task from tasks list in Marathon using rest API
我有一个 URL,其中 returns 我的应用程序的任务列表。
Request: /marathon/v2/apps/myapplication/tasks/
Response: {"tasks":[{"ipAddresses":[{"ipAddress":"172.17.0.3","protocol":"IPv4"}],"stagedAt":"2018-03-19T15:38:59.486Z","state":"TASK_RUNNING","ports":[22660],"startedAt":"2018-03-19T15:39:05.571Z","version":"2018-03-19T15:38:59.157Z","id":"myapplication.a47c2cd2-2b8b-11e8-a2bb-0e25310fd094","appId":"/myapplication","slaveId":"d0207878-76c3-4ce5-8d1a-f2cab319728c-S2","host":"10.134.52.126"}]}
响应中的任务字段是一个数组。如何从此数组中获取第一项?
您可以使用“jq”实用程序来解析 json 输入。
在您的情况下,它将是:
# To get First task details
cat response.json | jq '.tasks[0]'
# To get First task id
cat response.json | jq '.tasks[0].id'
# To get id of all tasks
cat response.json | jq '.tasks[] | .id'
我有一个 URL,其中 returns 我的应用程序的任务列表。
Request: /marathon/v2/apps/myapplication/tasks/
Response: {"tasks":[{"ipAddresses":[{"ipAddress":"172.17.0.3","protocol":"IPv4"}],"stagedAt":"2018-03-19T15:38:59.486Z","state":"TASK_RUNNING","ports":[22660],"startedAt":"2018-03-19T15:39:05.571Z","version":"2018-03-19T15:38:59.157Z","id":"myapplication.a47c2cd2-2b8b-11e8-a2bb-0e25310fd094","appId":"/myapplication","slaveId":"d0207878-76c3-4ce5-8d1a-f2cab319728c-S2","host":"10.134.52.126"}]}
响应中的任务字段是一个数组。如何从此数组中获取第一项?
您可以使用“jq”实用程序来解析 json 输入。 在您的情况下,它将是:
# To get First task details
cat response.json | jq '.tasks[0]'
# To get First task id
cat response.json | jq '.tasks[0].id'
# To get id of all tasks
cat response.json | jq '.tasks[] | .id'