javascriptjsonperformancejmeterjsonpath

JsonPath, how to get the records' ids with the minimal value of an attribute?


I have the following json data:

{
  "data": [
    {
      "ticket": "249 [DH-249]",
      "id": 249,
      "priority": 3,
      "title": "kUYWr7gWGH",
      "queue": {
        "title": "Django Helpdesk",
        "id": 1
      },
      "status": "Open",
      "created": "4\u00a0hours ago",
      "due_date": "13\u00a0hours ago",
      "assigned_to": "uUAKA3ZA 2rg8oS9X",
      "submitter": "ke89t9hy@gmail.com",
      "row_class": "",
      "time_spent": "",
      "kbitem": ""
    },
    {
      "ticket": "250 [DH-250]",
      "id": 250,
      "priority": 3,
      "title": "3kEpyPXp4",
      "queue": {
        "title": "Django Helpdesk",
        "id": 1
      },
      "status": "Open",
      "created": "4\u00a0hours ago",
      "due_date": "13\u00a0hours ago",
      "assigned_to": "None",
      "submitter": "cqgZHF7a@gmail.com",
      "row_class": "",
      "time_spent": "",
      "kbitem": ""
    }
  ],
  "recordsFiltered": 195,
  "recordsTotal": 195,
  "draw": 1
}

How can i get an array of IDs where the priority is equal to minimal? For example, if the minimal priority is 3 in the data, I need to get id=249, id=250 (there's more data usually)

I have tried using the JsonPath expression gpt gave me: $.data[?(@.priority == $.data[*].priority.min())].id but it does not work


Solution

  • I don't think you can get it using JSONPath as current implementation doesn't allow evaluating min() function properly.

    You can switch to JSR223 PostProcessor and use the following code to parse the response and extract all id attributes of the minimum priority:

    def data = new groovy.json.JsonSlurper().parse(prev.getResponseData()).data
    
    def minPriority = data.min { it.priority }.priority
    
    def ids = data.findAll(element -> element.priority == minPriority).id
    
    if (ids.size() == 1) {
        vars.put('id', ids.get(0) as String)
    } else {
        ids.eachWithIndex { id, index ->
            vars.put('id_' + (index + 1), id as String)
        }
    }
    

    More information: