Extracting json data from Jmeter http request response - regex

I can't find a way to extract array from a json response in Jmeter http request. All other fields i'm able to extract. However something like the following i cannot seem to extract. is there any way i can extract this from response in Jmeter http request? In the following i need to extract [1,2,3] and save it in a variable.
"days":[1,2,3]
"sectionIds":[abc,def,ghi]
something like the folliwing i can just extract using "subscriptionId":"(.+?)" in the jmeter regular expression extractor
"subscriptionId":"abcd"
time value can be extracted using this "time":(\d+)
"time": 120

I guess you have problem with square braces not being quotted. Try:
"days":(\[.+?\])
or if you want to match numbers inside something like:
"days":(\[(\d+,?)+\])

I don't think that Regex way is the best option to deal with JSON data.
There is a plugin called JSON Path Extractor which provides functionality alike to JMeter Embedded XPATH Extractor but targeting JSON.

Related

JMeter - How to extract query string and request headers?

I am working on log-in scenario for Jmeter. Once I logged-in, I am redirected to the below request:
https://analogb2c.b2clogin.com/analogb2c.onmicrosoft.com/B2C_1A_ADI_SignUpOrSignInWithKmsi/SelfAsserted?tx=StateProperties=eyJUSUQiOiJlOWRiNTU1Zi05N2ZmLTRmOTYtOGQwMC0yOTc2MGNlYmE4ZDYifQ&p=B2C_1A_ADI_SignUpOrSignInWithKmsi
How do I extract the Query String Parameters "tx" and "p" and "X-CSRF-TOKEN" that is in the request headers? What is the regular expression to get the values? I will use the value to the next succeding request. Thanks
Query String Parameters
For the tx it would be something like:
tx=(.+?)&
For the X-CSRF-TOKEN it would be something like:
X-CSRF-TOKEN:\s*(.*)
If you have problems with JMeter Regular Expressions - you can consider switching to the Boundary Extractor, it basically extracts everything between left and right boundaries, doesn't require any domain knowledge and works much faster. More information: The Boundary Extractor vs. the Regular Expression Extractor in JMeter

Extract location from response header in Jmeter

From the below string
https://ABC.somewebsitename.com/Account/AutoLogOn?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxMDEyMzk3ODMiLCJ0YXJnZXRDb3VudHJ5Q29kZSI6IkpQIiwiZXhwIjoiMTU3OTUxMjc4MiJ9.QhfDKvZXSrqTjGYsBaZUd6ErbYoOJUdta0efws2SI-nv0VYqUvByHvoKbWVGvd89RmOi-KV33CDHU8NE_Pl0NA
I want to extract the value after token and send it as a parameter in next request for that I used Regular Expression extractor as below
But in the next request parameter, I am not able to get those values
Am I missing something, can you help me on this?
Note: I have followed the link Extract Location from Response Header with JMeter but still no use.
Your regular expression is missing capturing group, I believe you should amend it like:
https.+token=(.*)
Demo:
However if you just need to extract the token and append it to the next request it is easier to consider Using the HTTP URL Re-writing Modifier
Regular expression should be token=(.+?)$
Also set the Field to Check to URL in the Regular Expression Extractor element.
I did some trial and error, and got the answer,
In the Regular Expression Extractor, used as
Regular Expression: Location: .+\/AutoLogOn\?token=(.*?)\n

Regular expression for JSON code

I'm trying to extract the access_token value.
anyone know how to extract this.
{"access_token":"c170f85d-9371-31c3-a4fa-1f0865ef28da","refresh_token":"6312ab1a-e257-38e9-aab1-8f8071dfd643","scope":"openid","id_token":"eyJ4NXQiOiJOVEF4Wm1NeE5ETXlaRGczTVRVMVpHTTBNekV6T0RKaFpXSTRORE5sWkRVMU9HRmtOakZpTVEiLCJraWQiOiJOVEF4Wm1NeE5ETXlaRGczTVRVMVpHTTBNekV6T0RKaFpXSTRORE5sWkRVMU9HRmtOakZpTVEiLCJhbGciOiJSUzI1NiJ9.eyJhdF9oYXNoIjoibTFGZUk4ZFJFbDV6azZoTFd2VUxBUSIsImNfaGFzaCI6IkRGbVY3RUt4dzlhck4xNFhDOUxQT1EiLCJzdWIiOiJzb2wxdXNlcjYwIiwiYXVkIjpbInRlc3RtYW5lZXNoYWNvZGUwMDAwMDAxMjMwMDAwIl0sImF6cCI6InRlc3RtYW5lZXNoYWNvZGUwMDAwMDAxMjMwMDAwIiwiYW1yIjpbIkJhc2ljQXV0aGVudGljYXRvciJdLCJpc3MiOiJodHRwczpcL1wvbG9jYWxob3N0Ojk0NDNcL29hdXRoMlwvdG9rZW4iLCJleHAiOjE1MzM3Mjk4MjYsImlhdCI6MTUzMzcyNjIyNiwic2lkIjoiZmE0NDNjZTMtNTI0MS00N2ZkLWIxYzYtMDkxNTU1ODNmZDcwIn0.J6ymae3s4UOvNRiJIL_kdni4HRgUoIHf4speGduQjGd_ny_3ow0MjxHW51wxJfAWpSw9a8cMfc5DkI0lgQmPoWUeNlQ1-loNOGPCMREE4PfJPG312psjxecIjU0j7fr8ma-wAkPKMicSK1VrrbURgU9XdVjve3iVOyAW7ypuGuIgOqwulQdYMldTVBrixWjQvjmygezV30V48OAY8oNIrp6nLWzBUV9w9Zb7_ASblLSezMceIecHMl-0EBAnMYvqgxtbk8aB* Connection #0 to host localhost left intact tLOsz3bqAonEMk3EvnWrQ7GsL2Mrtc_Wo2n7bfv3fkdjkeeb8fuVqUuC4nG-u2In9hdck8HOXoojZQ","token_type":"Bearer","expires_in":2881}
I need to get c170f85d-9371-31c3-a4fa-1f0865ef28da only from above response.
How can I get it using a regex?
Add a JSON extractor as a child to the request where you are getting above response
use $..access_token as JSON path expression.
Match no 1
give name to variable
TO access the variable you can use ${variablename}
For more info on regular expression extraction follow this link

Regex to extract consent form response data?

i am new to jmeter and i am using regular expression extractor to extract document number which is between
<showDocument> 834446$$$$1601Consent </showDocument>
which field should i check Body or Response Header
The relevant Regular Expression Configuration would be:
Reference Name: anything meaningful, i.e. showDocument
Regular Expression: <showDocument>(.+?)</showDocument>
Template: $1$
You can access extracted value as ${showDocument} where required. See Regular Expressions chapter of JMeter User Manual for more details
By the way, your response part looks like XML so it might be easier and better to consider using XPath Extractor instead. In that case XPath Tutorial would be extremely helpful.

How do I get data from below service response using regular expression extractor in Jmeter?

How do I get data from below service response using regular expression extractor in Jmeter?
Extract token ID:
<ValidateUserResponse xmlns="http://tempuri.org/"><ValidateUserResult xmlns:a="http://schemas.datacontract.org/2004/07/ISOS.Medtrack.Mobile.Entity" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:ErrorKey i:nil="true"/><a:ErrorMessage i:nil="true"/><a:SystemId>0</a:SystemId><a:Type>0</a:Type><a:status>SUCCESS</a:status><a:IsAuthenticated>true</a:IsAuthenticated><a:IsDashboardLanding>true</a:IsDashboardLanding><a:IsEmployee>true</a:IsEmployee><a:IsMobileAppEnabled>true</a:IsMobileAppEnabled><a:IsPwdChgRqd>false</a:IsPwdChgRqd><*a:TokenID>**9de2dbfbcc94241002f57275e8dc78e60f26baabc05c1eebe39088d02e6e454f***</a:TokenID><a:UserStatus>ACTIVE</a:UserStatus></ValidateUserResult></ValidateUserResponse>
Theoretically you can do it with regular expressions, however I would rather go for XPath Extractor for dealing with XML data instead.
Follow the next steps to extract your TokenID value:
Add the following line to user.properties file (lives under /bin folder of your JMeter installation)
xpath.namespace.config=my.properties
Create "my.properties" file in the same location and add next 2 lines there:
a=http://schemas.datacontract.org/2004/07/ISOS.Medtrack.Mobile.Entity
i=http://www.w3.org/2001/XMLSchema-instance
Restart JMeter
Add XPath Extractor as a child of the request which returns that XML
Configure the XPath Extractor as follows:
Check "Use Namespaces" box
Put //a:TokenID/text() line into "XPath Query" input
Set "Reference Name" to something meaningful, i.e. TokenID
You should be able to access extracted value as ${TokenID} after XPath Extractor execution.
Hope this helps.
Though Regex should not be used in XML cases as it is a bad practice.
but XPath Extractor should be avoided in actual performance tests because they are heavy in nature as compared to Regex.
XPath builds a DOM tree so it consumes CPU and memory and you might run out of memory or overload CPU during performance tests which in turn could result in invalid results. Thus prefer Regular Expression extractor whenever possible.
Simple Regex expression could be,
TokenID>\*\*(\w+)\*\*\*
extracted value can be used as $1$.
The best way to solve the problem with namespaces in XPath Extractor, that simple use the impruved iXpath (improved jmeter xpath plugins)
http://sourceforge.net/p/i-jmeter-xpath/wiki/Home/