1

Given the query

@.records[*].issues[].[type, message]

on the JSON

{
   "records":[
      {
         "id":"db7bb828-60e2-5fa8-048c-06542abd98d2",
         "parentId":"3dc8fd7e-4368-5a92-293e-d53cefc8c4b3",
         "type":"Task",
         "name":"PublishBuildArtifacts",
         "startTime":"2022-09-28T14:06:41.3266667Z",
         "finishTime":"2022-09-28T14:06:41.3266667Z",
         "currentOperation":null,
         "percentComplete":null,
         "state":"completed",
         "result":"skipped",
         "resultCode":"Evaluating: SucceededNode()\r\nResult: False\r\n",
         "changeId":29,
         "lastModified":"0001-01-01T00:00:00",
         "workerName":"AgentSalam7WithPat",
         "order":19,
         "details":null,
         "errorCount":0,
         "warningCount":0,
         "url":null,
         "log":null,
         "task":{
            "id":"2ff763a7-ce83-4e1f-bc89-0ae63477cebe",
            "name":"PublishBuildArtifacts",
            "version":"1.158.3"
         },
         "attempt":1,
         "identifier":null
      },
      {
         "id":"d56f7c92-f706-53be-685b-17b89c98baa6",
         "parentId":"3dc8fd7e-4368-5a92-293e-d53cefc8c4b3",
         "type":"Task",
         "name":"SonarQubePublish",
         "startTime":"2022-09-28T14:06:31.7066667Z",
         "finishTime":"2022-09-28T14:06:41.31Z",
         "currentOperation":null,
         "percentComplete":null,
         "state":"completed",
         "result":"failed",
         "resultCode":null,
         "changeId":31,
         "lastModified":"0001-01-01T00:00:00",
         "workerName":"AgentSalam7WithPat",
         "order":11,
         "details":null,
         "errorCount":1,
         "warningCount":0,
         "url":null,
         "log":{
            "id":14,
            "type":"Container",
            "url":"https://azuredevops2k19.salam.net/Sierac-Utilities/6f9f1b22-cd2b-4ed4-a2c9-37822128b7c6/_apis/build/builds/201/logs/14"
         },
         "task":{
            "id":"291ed61f-1ee4-45d3-b1b0-bf822d9095ef",
            "name":"SonarQubePublish",
            "version":"5.0.1"
         },
         "attempt":1,
         "identifier":null,
         "issues":[
            {
               "type":"error",
               "category":"General",
               "message":"[SQ] Task failed with status FAILED, Error message: Fail to extract report AYOEa2gdtfNdJFd6edM9 from database",
               "data":{
                  "type":"error",
                  "logFileLineNumber":"9"
               }
            },
            {
               "type":"warning",
               "category":"General",
               "message":"Unable to get default branch, defaulting to 'master': Error: enable to verify the first certificate",
               "data":{
                  "type":"warning",
                  "logFileLineNumber":"10"
               }
            }
         ]
      }
   ]
}

I get the resulting JSON:

[
  [
    "error",
    "[SQ] Task failed with status FAILED, Error message: Fail to extract report AYOEa2gdtfNdJFd6edM9 from database"
  ],
  [
    "warning",
    "Unable to get default branch, defaulting to 'master': Error: enable to verify the first certificate"
  ]
]

Now I need to add a filter like [type = error], so I only get the messages of type error.
How can this be achieved? In the documentation, this is not very clear to me.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
eliassal
  • 343
  • 5
  • 20

1 Answers1

0

Filtering and multiselect lists do need a question mark in the array notation brackets – [?this > `that`] – and the equality test is a double equal sign – ==.

So your query should be:

@.records[*].issues[?type == `error`].[type, message]

Which gives the resulting JSON:

[
  [
    [
      "error",
      "[SQ] Task failed with status FAILED, Error message: Fail to extract report AYOEa2gdtfNdJFd6edM9 from database"
    ]
  ]
]

Should you need to flatten the multiple arrays of arrays, you can use the flatten operator, and with the query:

@.records[*].issues[?type == `error`].[type, message][][]

You will, then, end up with this resulting JSON:

[
  "error",
  "[SQ] Task failed with status FAILED, Error message: Fail to extract report AYOEa2gdtfNdJFd6edM9 from database"
]
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83