JMeter – Working with JSON – Extract JSON response


This post is about 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
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
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
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.

Comments

Popular posts from this blog

JMeter – Working with JSON

Existence: Absolute & Relative

Hyperlocal Startups Creating a Better Living