使用rest API从Marathon中的任务列表中获取任务

时间:2018-03-19 16:34:27

标签: marathon dcos

我有一个URL,它返回我的应用程序的任务列表。

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"}]}

响应中的任务字段是一个数组。如何从这个数组中获取第一项?

1 个答案:

答案 0 :(得分:0)

您可以使用“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'