Quintus Chen
Quintus Chen

Reputation: 53

Jmeter JSON Extractor retrieve the second item from last of a list

I have a JSON response like below

{
    "queryStartDate": "20170523134739822",
    "queryEndDate": "20170623134739822",
    "Rows": [
        {
            "hasScdHistoryOnly": false,
            "Values": [
                       "1",
                       "53265",
                       "CO"
            ]
        },
        {
            "hasScdHistoryOnly": false,
            "Values": [
                       "1",
                       "137382",
                       "CO"
                      ]
        },
        {
            "hasScdHistoryOnly": false,
            "Values": [
                       "1",
                       "310824",
                       "CO"
                      ]
        }
    ]
}

I am using Jmeter's JSON Extractor post-processor to receive the second value from the last of the 'Values' list. i.e. 53265, 137382, 310824.

I've tried to use $.Rows[*].Values[-2:-1], and $.Rows[*].Values[(@.length-2)], according to Stefan's introduction: http://goessner.net/articles/JsonPath/index.html#e2, but neither of them are working. Would you please help me out?

Upvotes: 0

Views: 2052

Answers (2)

Dmitri T
Dmitri T

Reputation: 168157

I believe JMeter is using JayWay JSON Path library, so you should be looking for the documentation here instead.


In general I would recommend using JSR223 PostProcessor as an alternative to JSON Path Extractors, both are applicable for basic scenarios only, when it comes to advanced queries and operators their behaviour is flaky.

  1. Add JSR223 PostProcessor as a child of the request which returns above JSON
  2. Make sure you have "groovy" selected in the "Language" drop down and "Cache compiled script if available" box is ticked
  3. Put the following code into "Script" area

    def values = com.jayway.jsonpath.JsonPath.parse(prev.getResponseDataAsString()).read('$..Values')
    
    values.eachWithIndex { val, idx ->
        vars.put('yourVar_' + (idx + 1), val.get(val.size()-2))
    }
    
  4. It should generate the following JMeter Variables:

    yourVar_1=53265
    yourVar_2=137382
    yourVar_3=310824
    

    which seem to be something you're looking for.

References:

Upvotes: 3

Adnan
Adnan

Reputation: 2547

Using View Results tree's JSon Path Tester I could see that the following expression you used for extracting the values were not correct (correct for online JSONPath Online Evaluator but not working for JMeter)

Used Expression: $.Rows[*].Values[-2:-1]

Output from JSon Path Tester: No Match Found. enter image description here

Used Expression: $.Rows[*].Values[(@.length-2)]

Output from JSon Path Tester: Exception: Could not parse token starting at position 16. Expected ?, ', 0-9, *enter image description here

If the expression $.Rows[*].Values[1] is used it extracts the desired responses.

Used Expression: $.Rows[*].Values[1]

Output from JSon Path Tester:

 Result[0]=53265
 Result[1]=137382
 Result[2]=310824

enter image description here

Upvotes: 0

Related Questions