在bash脚本中使用jq处理json

时间:2018-10-12 21:26:54

标签: bash jq

以下是我需要在bash脚本中使用jq处理的json。我需要获取“ Id”列值。由于此json中有3条记录,因此将返回具有最大值ID的记录。所以在处理以下json之后,我应该得到170。 我是新手,接触bash的机会非常有限。

{
  "count": 3,
  "value": [
    {
      "properties": {},
      "tags": [],          "validationResults": [],
      "plans": [
        {
          "planId": "49699e0f-b893-4633-bc05-754b8a562d07"
        }
      ],          "triggerInfo": {},
      "id": 170,
      "buildNumber": "20181011.8",          "status": "completed",          "result": "succeeded",          "queueTime": "2018-10-11T15:56:24.9611153Z",          "startTime": "2018-10-11T15:56:28.3668144Z",          "finishTime": "2018-10-11T15:57:20.5163422Z",
      "url": "https://indiatelecom.visualstudio.com/d354caa2-2e88-414a-829b-25df3aceaaaf/_apis/build/Builds/170",
      "buildNumberRevision": 8,          "uri": "vstfs:///Build/Build/170",
      "sourceBranch": "refs/heads/master",          "sourceVersion": "4303c19f8fda79e35fcb598219d5dca6bb274c2d",
      "priority": "normal",          "reason": "manual",          "lastChangedDate": "2018-10-11T15:57:20.797Z",          "parameters": "{\"system.debug\":\"false\"}",
      "orchestrationPlan": {
        "planId": "49699e0f-b893-4633-bc05-754b8a562d07"
      },          "keepForever": false,          "retainedByRelease": false,          "triggeredByBuild": null
    },
    {          "properties": {},          "tags": [],          "validationResults": [],
      "plans": [         {              "planId": "15026a2f-c725-4e52-974b-61e01a940661"
        }          ],
      "triggerInfo": {},
      "id": 160,
      "buildNumber": "20181009.20",          "status": "completed",          "result": "succeeded",          "queueTime": "2018-10-09T16:47:42.2954075Z",          "startTime": "2018-10-09T16:47:43.8034575Z",
      "finishTime": "2018-10-09T16:48:35.8340469Z",          "url": "https://indiatelecom.visualstudio.com/d354caa2-2e88-414a-829b-25df3aceaaaf/_apis/build/Builds/160",
      "buildNumberRevision": 20,          "uri": "vstfs:///Build/Build/160",
      "sourceBranch": "refs/heads/master",          "sourceVersion": "19a55c7482083785265b86015150521b40230c11",
      "priority": "normal",          "reason": "manual",
      "lastChangedDate": "2018-10-09T16:48:36.057Z",          "parameters": "{\"system.debug\":\"false\"}",
      "orchestrationPlan": {
        "planId": "15026a2f-c725-4e52-974b-61e01a940661"
      },
      "keepForever": false,          "retainedByRelease": false,
      "triggeredByBuild": null        },
    {
      "properties": {},          "tags": [],
      "validationResults": [],          "plans": [
        {
          "planId": "e45d9da8-4d95-42b7-aa23-478e1c1c49f5"
        }
      ],
      "triggerInfo": {},
      "id": 147,
      "buildNumber": "20181009.7",          "status": "completed",
      "result": "succeeded",          "queueTime": "2018-10-09T15:15:47.0248009Z",
      "startTime": "2018-10-09T15:15:50.8899892Z",          "finishTime": "2018-10-09T15:16:47.7866356Z",
      "url": "https://indiatelecom.visualstudio.com/d354caa2-2e88-414a-829b-25df3aceaaaf/_apis/build/Builds/147",
      "buildNumberRevision": 7,          "uri": "vstfs:///Build/Build/147",
      "sourceBranch": "refs/heads/master",          "sourceVersion": "70fccb138a2f2a9dfe18290c468959102f504067",
      "priority": "normal",          "reason": "manual",
      "lastChangedDate": "2018-10-09T15:16:48.16Z",
      "parameters": "{\"system.debug\":\"false\"}",          "orchestrationPlan": {
        "planId": "e45d9da8-4d95-42b7-aa23-478e1c1c49f5"
      },          "keepForever": false,          "retainedByRelease": false,
      "triggeredByBuild": null        }
  ]    }

1 个答案:

答案 0 :(得分:3)

ID存储在键value下的数组中。 .value[].id列出ID,如果将它们放入数组,则可以在其上调用max

jq '[.value[].id] | max' < file.json