Parth Tiwari
Parth Tiwari

Reputation: 486

How to return the last id in JSON using JSON Extractor in JMeter

Here is my response code I want to retrieve the last id always using JSON extractor and further how can I pass this id to another HTTP Request in POST parameter

        {
            "response": [
                {
                    "dm": "CL 6",
                    "id": 3,
                    "yN": "t1-cl -6"
                },
                {
                    "dm": "CL 2",
                    "id": 4,
                    "yN": "t1- cl -2"
                }
            ],
            "timestamp": "2020-04-02T22:40:42.416",
            "status": "OK"
        }

What I have tried in JSON extractor

enter image description here

but it does not return anything. I even tried $..id[-1]

further, I need to pass this id to another HTTP Request in POST parameter

Upvotes: 0

Views: 230

Answers (2)

Dmitri T
Dmitri T

Reputation: 168157

Your configuration should generate the following JMeter Variables:

extract_id_1=3
extract_id_2=4
extract_id_matchNr=2

enter image description here

So the "last" ID can be obtained using __V() function as follows:

${__V(extract_id_${extract_id_matchNr})}

Demo:

enter image description here

More information: Here’s What to Do to Combine Multiple JMeter Variables

Upvotes: 0

Masud Jahan
Masud Jahan

Reputation: 2978

Use this as JSON Path Expressions: $.response[-1:].id

This will return the last id from the response array objects.

enter image description here

Upvotes: 1

Related Questions