How to extract 1 value from multiple JSON bodies in JMeter JSON Extractor?

40 views Asked by At

Hope all is well with you guys.

In JMeter, I have a thread group to add items to cart for 2 users which results in 2 responses with 2 different bodies and different Cart IDs.

What I want is to extract each Cart ID value from each response and save it so I can use it in another thread group using BeanShell.

Here's a sample:

I have added 2 items to 2 different users, and when using JSON Extractor, it gets the CartID for Cart 2 (https://i.stack.imgur.com/zQUf0.png)

How can I get the cart IDs for all responses sequentially?

Here's my JSON Extractor setup:

(https://i.stack.imgur.com/4vENE.png)

1

There are 1 answers

0
Dmitri T On

It's not possible to say what's wrong without seeing the code in your Beanshell Assertion.

If you use something like props.put("someid", vars.get("CartID"));

then you're in a trouble because JMeter properties are global and each subsequent call simply overwrites the previous value. If you want to continue this way you could use current thread number as a prefix or a postfix for the property name, i.e.

props.put("someid" + ctx.getThreadNum(), vars.get("CartID"));

Personally I would rather go for Inter-Thread Communication Plugin

Also be informed that using Beanshell is a kind of a performance anti-pattern, since JMeter 3.1 you're supposed to use JSR223 Test Elements and Groovy language for scripting.