Extracting a value from JSON with JMeter

So again an issue I had and thought I’d share. I found very little about this on Google. The problem I had was, that I got a JSON response from a site that contained an ID I’d need to use on the next page. The challange was that I had a variable in the regex which JMeter doesn’t seem to like. The easiest way to do this is to use JMeter-plugins!!!

Short post, if it were not for the unlucky fact that I couldn’t use the plugins. So here the long answer….

Like before the easierst (in my opinion) is to pull out your Groovy foo and do something like the below in a Post Processor.  1st off the JSON:

    "WhatINeedID": 0000001;
    "SomeData": "Test Data"
    "WhatINeedID": 0000005;
    "SomeData": "Test Data"
    "WhatINeedID": 0000011;
    "SomeData": "Test Data"

What I need to extract is the WhatINeedID for ID “HELLOWORLD0002”. The ID is saved in a JMeter variable ${SearchID}. A regex would look something like this in JMeter:


Looks simple but JMeter barfs at that because of the variable. And what do you do if you have several values you want to extract?
The below Groovy script gives you that flexibility. It hinges on the JSONslurper library which comes with Groovy.

import org.apache.jmeter.samplers.SampleResult; 
import groovy.json.JsonSlurper;

String data     = prev.getResponseDataAsString();
String SearchID = vars.get('SearchID');

def slurper = new JsonSlurper();
def result  = slurper.parseText(data);
def index   = result.find { it.ID == SearchID };

if ( index == null ) {
    vars.put( 'WhatINeedID', 'NOTFOUND' );
} else {
    vars.put( 'WhatINeedID', index.WhatINeedID.toString() );

Neat and simple, just took ages to figure out. There would be a variable WhatINeedID in JMeter now with the content 0000005.

This would be something you could use if you are sure there is a maximum of one occurrence. If you have more use the index.each function to iterate over the result. Worth mentioning is that index has the full sub-JSON that it found. So in this case would look like this:

{"ID":"HELLOWORLD0002";"WhatINeedID": 0000005;"SomeData":"Test Data"}

And you can search again or do whatever JSON nastiness you want with it.


by Oliver Erlewein


2 thoughts on “Extracting a value from JSON with JMeter

  1. Thanks a lot, I learned that JSON parser is built into groovy, good to know that no extra libraries are required.

    Actually you can do it without scripting using the JSONPath Extractor plugin mentioned by you. The relevant JSON Path Expression will be:


    See JSONPath – XPath for JSON for JSONPath to XPath mapping and Using the XPath Extractor in JMeter for general information on parsing XPath and JSON in JMeter tests.

  2. Pingback: The Weekly RoundUp! 10 Great Questions from Software Tester to Architect and other community news! | bmod

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s