vlatko606
vlatko606

Reputation: 1149

Jmeter - How to output all the values extracted from JSON extractor

I have json with dynamic range which is groving all the time.

{
    "payload": {
        "totalCount": 302,
        "items": [
            {
                "id": 1379,
                "date": "2021-04-08T17:06:03+02:00",
                "name": "Newcastle United Aston Villa",
                "externalId": "2",
                "mediaTag": null,
                "deleted": false,
                "originalId": null,
                "cancelled": false,
                "relatedEventsOrderIndex": []
            },
            {
                "id": 1380,
                "date": "2021-04-08T17:06:03+02:00",
                "name": "Arsenal Tottenham Hotspur",
                "externalId": "3",
                "mediaTag": null,
                "deleted": false,
                "originalId": null,
                "cancelled": false,
                "relatedEventsOrderIndex": []
            }
            
            
        ]
    },
    
    "errorCode": 0,
    "errorTimeStamp": null,
    "errorMessage": null,
    "hasError": false
}

I can extract all the values like

enter image description here

And if i use as: ID1_1, ID1_2.......ID1_100 -> its working fine.

But, I want to output in the logger. If I tried as:

def IDS = vars.getObject("ID1");

for (int i=0; i>IDS.length; i++) {
    
    log.warn("ID as: " + IDS + (i + 1) );
}  

I got: Cannot get property 'length' on null object

How can I output all the ID, from dynamic JSON?

Upvotes: 2

Views: 1614

Answers (2)

Dmitri T
Dmitri T

Reputation: 168157

I don't think you have ID1 variable, you have ID1_1, ID1_2 .... and ID1_matchNr which holds the number of matches, you can see it yourself using Debug Sampler

You need to change your code as:

for (int i = 1; i <= vars.get('ID1_matchNr') as int; i++) {
    log.warn("ID as: " + vars.get("ID1_" + i));
} 

in the above code vars stands for JMeterVariables class instance

Upvotes: 2

Rahul Jadhav
Rahul Jadhav

Reputation: 463

It should be something like this (pseudo code)

for (int i=0; i>IDS.length; i++) {
    
    log.warn("ID as: " + vars.get("ID1_" + i.toString() );
}  

each of the ID1_1, ID1_2.......ID1_100 are variables. You can verify that using a Debug Sampler

Upvotes: 1

Related Questions