JMeter – Working with JSON – Extract JSON response
Ok,
like i promised in a previous post of mine, talking about working with JSON and JMeter, i will discuss using the JSON response for extracting specific values, to be further used in the test script.
Now, there is no rocket science in this. Sending an HTTP Request with a JSON string as payload, will return a JSON string also, built on the same convention as already discussed. Now, the only thing you need is to extract the information you need from this response. I consider the Regular Expression Extractor to be the best option (actually i haven’t even given any other try
)
So, let us suppose you get the following JSON formatted response:
{ “books” { “name”:”Starwars”,”pages”:500},{“name”:”Lord of the rings”,”pages”:1500}}
and you need to extract the book names, and use them one by one in a For Each Controller.
The For Each Controller works perfectly with the regular expression extractor, in a sense that it will loop through the values that the regular expression extractor returned.
Let’s start with the regular expression extractor.
Select the HTTP Request that you just used for sending the request, with the JSON payload. Add a new regular expression extractor (Post Processors) As you are interested in the book names, you will search for the following regular expression:
“name”:”(.+?)”
The special characters above are:
( and ) – these enclose the portion of the match string to be returned ; . – match any character. + – one or more times. ? – don’t be greedy, i.e. stop when first match succeeds
Note: without the ?, the .+ would continue past the first ” until it found the last possible ” – probably not what was intended.
Use -1 as match number for instructing JMETER to store all results in an array, in order to be further used with a ForEachController
Use $1$ as template, as you are extracting only one item from the response ( if you were interested in extracting also the pages, you would use $1$$2$, and also change the regular expression to “name”:”(.+?),”pages”:(.+?) )
Your jmeter regular expression extractor should look something like this:

JMeter Regular Expression Extractor
Now, you would use the BOOKS variable as input in the for each controller.

JMeter Regular expression in for each controller
Bookname will now be your output variable, that you can further on use in the http requests contained in your for each controller, as ${BOOKNAME}

JMeter Http Request
Addressing a specific element in the results array can be done also, but you would have to give up on your for each controller (of course), and modify your regular expression. JMeter documentation has pretty good tips on doing that.
Basically, extracting specific values from a JSON string is nothing else but extracting values from any server response, be it xml, plain html, json ,etc.. You just need to dig in a little on the regular expression subject. That’s pretty much it. Hope this helps.
Cheers,
Alex
thx a lot alex, i was struggling with this issue a lot of time. Good job
glad to be of help.
Thanks for your useful post. Actually I want to do the same as you with JMeter but have some problems with that. I want to extract the value of item_id in the following response as use it as variable for the next request:
{“programs”: [
{
"item_id": "49990415200912151320",
"channel_id": "TRT 1",
"title": "Hava Durumu",
"language": "tr-TR",
"description_short": "Trkiye ile dnyann.",
"start_time": "2009-12-15T13:20:00+02:00",
"end_time": "2009-12-15T13:30:00+02:00",
"credits": {"producer": [{"name": "TRT"}]},
“genre”: [{"name": "Hava Durumu"}]
},
{
“item_id”: “88101815200912152350″,
“channel_id”: “TRT 1″,
“title”: “Naslsnz?”,
“language”: “tr-TR”,
“description_short”: “Tayfun Talipolunun.”,
“start_time”: “2009-12-15T23:50:00+02:00″,
“end_time”: “2009-12-16T00:40:00+02:00″,
“credits”: {“director”: [{"name": "Ayhan Arabac"}]},
“genre”: [{"name": "Aktalite Program"}]
}
]}
But can’t find the right regex to extract the number of item_id. Actually its the same as your JSON text but it doesn’t work with your supposed regex “item_id”:”(.+?)”
Do you have any idea?
Thanks in advance!
Babak
Hi Babak,
are you trying to use the values in a foreach controller?
How about doing a little bit of debugging, and instead of “-1″ for the match number, setting “1″ ? This should return the first occurence from the array of values. You can then test to see if the regex has worked at all, using your output variable, and see if the problem is somewhere else.
I double checked it once again in my script. I am also extracting a unique id, and it looks like this:
Reference name : MYSESSION
Regular Expression : “sessionID”:”(.+?)”
Template: $1
Match no: 5 ( i am interested specifically on this id)
Default Value: REGEX_FAILED (you should see this as output value if the regex above failed)
Let me know if it worked for you. Otherwise i can double check your testplan.
Rgds,
Alex
First of all great Post!
I think I am a step further but I have been struggling with a JSON with some properties.
For example, I have JSON of the followings.
{ “books” { “name”:”Starwars”,”rating”:”good”,”pages”:500},{“name”:”Lord of the rings”,”rating”:”very good”, ”pages”:1500}{ “name”:”Starwars 2″,”rating”:”superb”, “pages”:500},{ “name”:”Lord of the rings 2″,”rating”:”excellent”, “pages”:1500}}
I am trying to get names of the book with 500 pages, i would think that my regex looks like:
“name”:(.*).*”pages”:500
but ended up matching these strings:
“name”:”Starwars”,”rating”:”good”,”pages”:500},{“name”:”Lord of the rings”,”rating”:”very good”, ”pages”:1500}{ “name”:”Starwars 2″,”rating”:”superb”, “pages”:500
was hoping that I could get these:
“name”:”Starwars”
“name”:”Starwars 2”
So I can pass them in my for each controller.
However, seems like I couldn’t get a lazy match that would match the shortest “name”.
Let me know if you have any idea how to do that.
Thanks! :>
hi yudi, i will answer your post as soon as i can. kind of accesing my net from an ipod right now. sorry about that.
i will come back asap
cheers,
alex
Hi,
why bother with Regexp PostProcessor for JSON result parsing?
It is much easier to use a BSF PostProcessor with javascript to extract what you need.
{code}
var o = eval(“(” + prev.getResponseDataAsString()+ “)”);
var bookNames = new Array(30);
for(var i=0; i<o.books.length; i++) {
bookNames[i] = o.books[i].name;
}
vars.put("BOOOKNAME", bookNames);
{code}
brg
/arvid
Hi Alex,
I am trying to use regular extractor to extract the room id from a response message.
The response typically looks like this-
{“result”:{“stayEndDate”:”2010\/10\/06″,”extraDetailsList”:[{"hidden":null,"parameters":[],”name”:”Room Cancellation Insurance”,”displayOrder”:1,”extraOptions”:[{"value":"0","name":"No Room Cancellation Insurance","id":1,"displayOrder":1,"description":null,"preSelected":null},{"value":"1.5","name":"Room Cancellation Insurance ","id":2,"displayOrder":2,"description":null,"preSelected":true}],”helpURL”:null,”parameterised”:null,”altDescription”:null,”rooms”:[{"name":null,"id":30001955}],”id”:1,”subDescription”:null,”description”:”Cost: \u00a31.50 per stay\r\nPrice includes Insurance Premium Tax at current rate.\r\nNote: Room Cancellation Insurance is available to UK Residents only.”,”userInputRequired”:null,”showWithRoom”:null},{“hidden”:null,”parameters”:[],”name”:”Breakfast Bag”,”displayOrder”:2,”extraOptions”:[{"value":"0","name":"No Breakfast Bag","id":14,"displayOrder":1,"description":null,"preSelected":true},{"value":"4.05","name":"Breakfast Bag","id":15,"displayOrder":2,"description":null,"preSelected":null}],”helpURL”:null,”parameterised”:null,”altDescription”:null,”rooms”:[{"name":null,"id":30001955}],”id”:6,”subDescription”:null,”description”:”Fancy a delicious breakfast to go? Our handy Breakfast Bag includes a cereal pot and milk, a muffin, croissant, jam portion and a fruit juice – which counts toward 1 of your 5 a day!”,”userInputRequired”:null,”showWithRoom”:null},{“hidden”:null,”parameters”:[],”name”:”WiFi”,”displayOrder”:6,”extraOptions”:[{"value":"0","name":"No Wi-Fi","id":3,"displayOrder":1,"description":null,"preSelected":true},{"value":"5","name":"1: Wi-Fi Access 60 minutes","id":4,"displayOrder":2,"description":null,"preSelected":null},{"value":"10","name":"2: Wi-Fi Access 24 hours","id":5,"displayOrder":3,"description":null,"preSelected":null},{"value":"20","name":"3: Wi-Fi Access 1 week","id":6,"displayOrder":4,"description":null,"preSelected":null},{"value":"30","name":"4: Wi-Fi Access 1 month","id":7,"displayOrder":5,"description":null,"preSelected":null}],”helpURL”:null,”parameterised”:null,”altDescription”:null,”rooms”:[{"name":null,"id":30001955}],”id”:2,”subDescription”:null,”description”:”To many people, the internet is now as essential as a comfy bed in their hotel room. So, when you choose WiFi access, your confirmation email will include a non-refundable personalised WiFi voucher. You\u2019ll need this when you get to the hotel \u2013 reception will help you get set up. And it\u2019s valid for 12 months, so if you have time left on your voucher you can use it stay after stay after stay.”,”userInputRequired”:null,”showWithRoom”:null},{“hidden”:null,”parameters”:[{"type":0,"name":"Mobile Number"}],”name”:”SMS”,”displayOrder”:7,”extraOptions”:[{"value":"0","name":"No SMS","id":20,"displayOrder":1,"description":null,"preSelected":true},{"value":"0.15","name":"SMS Confirmation required","id":21,"displayOrder":2,"description":null,"preSelected":null}],”helpURL”:null,”parameterised”:null,”altDescription”:null,”rooms”:[{"name":null,"id":30001955}],”id”:9,”subDescription”:null,”description”:”We will send you a text with details of your stay (Hotel, date of arrival, number of nights, booking reference etc). “,”userInputRequired”:null,”showWithRoom”:null},{“hidden”:true,”parameters”:[],”name”:”Pets”,”displayOrder”:8,”extraOptions”:[{"value":"0","name":"No Pets","id":8,"displayOrder":1,"description":null,"preSelected":true},{"value":"20","name":"1 Domestic Pet","id":9,"displayOrder":2,"description":null,"preSelected":null},{"value":"40","name":"2 Domestic Pets","id":24,"displayOrder":3,"description":null,"preSelected":null}],”helpURL”:null,”parameterised”:null,”altDescription”:”They\u2019re as much a part of the family as anyone else, so why not bring them along? Click here to find out more (it\u2019s a pop up, so you won\u2019t lose this page).”,”rooms”:[{"name":null,"id":30001955}],”id”:3,”subDescription”:”They\u2019re as much a part of the family as anyone else, so why not bring them along? Click here to find out more (it\u2019s a pop up, so you won\u2019t lose this page).”,”description”:”You can bring up to 2 pets with you on your stay. For now, we can only accommodate domestic cats and dogs (but you can be sure they\u2019ll get a good night\u2019s sleep too). Of course, Guide Dogs and Hearing Dogs are exempt from this charge.”,”userInputRequired”:null,”showWithRoom”:null}],”currency”:”£”,”outRoomBookingDetails”:null,”totalStayRoomCost”:null,”stayHotelName”:”Aberdeen Airport”,”stayStartDate”:”2010\/10\/05″,”rooms”:[{"name":"Double Room","id":30001955}],”totalRoomPrice”:”62.50″,”stayRooms”:”1″}}
I need to extarct id from this string “rooms”:[{"name":null,"id":30001955}].
I have used this regular expression
Reference name: Roominfo,BookingRoomId
Regular expression: “rooms”:(.*),”id”:(.+[^\W])
Template: $1$$2$
Match no: 0
But, this is not working.
Can you please help me in extracting the id please?
Thanks in advance,
-Suresh
Hi Alex,
appreciate your help with this one. I get a json response say {“p”:[{"x1":[{"a11":...,"b11":...,"c11":...},{"a12":...,"b12":...,"c12":...}]},{“x2″:[{"a21":...,"b21":...,"c21":...}]},…]} . From this json object of json arrays, I need to extract “x”.
-Im unable to extract x ie,. x1 or x2.
Further I actually need to perform a foreach operation on x within which I perform another foreach operation on a ie., a1, a12..
Thank you.
Hi Harini,
that’s a harder (nice) one to crack.
I’ll try to sort it out when i have some free time in the week-end…
Alex
am getting the following json exception while tryin to login using jmeter,,, plss help..invalid json string… and the values that recorded in jmeter are
{“userName”:”user8404″,”password”:”sfqatest”,”validationCode”:”prws6″}.. pls help
You need to check the exception on the server side, not on the client side. There is a reason why the server says that your json string is not valid.
you mentioned:( if you were interested in extracting also the pages, you would use $1$$2$, and also change the regular expression to “name”:”(.+?),”pages”:(.+?) )
But I am having trouble trying to figure out how to access the variables. When my template is just $1$ I dont have any problems, but with a template of $1$$2$ I dont know how to access the variables in the do while loop. Please help.
I use http://jakarta.apache.org/oro/demo.html to make sure my regular expression is correct.
Right. By changing the regular expression to name”:”(.+?),”pages”:(.+?) you now have a “double result”.
Let’s suppose you store the result in a variable called “MYRESULT” That means, if the regular expression result is:
test 1
then the result will be stored in the “MYRESULT” variable as “test1″
If you want to refer to the value “text”, then you use ${MYRESULT_g1}
If you want to refer to the value “1″, then you use ${MYRESULT_g2}
Quote:”If the match number is set to a non-negative number, and a match occurs, the variables are set as follows:
refName – the value of the template
refName_gn, where n=0,1,2 – the groups for the match
refName_g – the number of groups in the Regex (excluding 0)”
Try extracting the value of the variable first, and go on from there.
let me know if you get any results.
I think I figured it out now. I put a debug thing to dump the variables and this is how JMeter has them stored:
MYRESULT_?_g?
MYRESULT_1_g1 = book1name
MYRESULT_1_g2 = book1pages
MYRESULT_2_g1 = book2name
MYRESULT_2_g2 = book2pages
MYRESULT_3_g1 = book3name
MYRESULT_3_g2 = book3pages
Thanks for your help.
Hi Alex,
Any idea how to extract a value from an array, e.g. [0,0,0,0,0,12,0,0,0,456,0,33,3,[],34,1234,0,0,0,0,0,0] THX
Thanks, it really helps me alot to me.
Your blog is so great:
My JSON response is:
[
: {
: : "type":"rpc",
: : "tid":6,
: : "action":"campaignController",
: : "method":"create",
: : "result":
: : {
: : : "success":true,
: : : "total":1,
: : : "records":
: : : [
: : : : {
: : : : : "id":"CPGN-079570-11864256-437",
: : : : : "version":0,
: : : : : "name":"0!Create Sreepal Load Test Camp 500",
: : : : : "sequenceIds":null,
: : : : : "parentId":"CNVS-079532-15306089-005",
: : : : : "description":"",
: : : : : "referenceId":null,
: : : : : "status":"NEW",
: : : : : "startTime":0
: : : : }
: : : ],
: : : “message”:”Campaign created successfully”
: : }
: }
]
if use following expression:
Match count: 1
Match[1][0]=”id”:”CPGN-079570-11864256-437″
Match[1][1]=CPGN-079570-11864256-437
I am getting above results: If use id_g1 i am able to pass CPGN-079570-11864256-437
But i have to pass with quotations
could you please advise me the expression for my requirement
Hi Sreepal,
been on holliday for a couple of weeks. Sorry for the late reply. May i see the reg expression you are using?
Hi Alex,
How was your holiday. I hope it was great. I am able to solve the above issue by using “id”:(……………………..+?) expression.