Jmeter - How to extract value from the request (not response) using RegEx - jmeter

I have JSON request as following:
{
"type": "SIGNUP",
"data": {
"userAccountInfo": {
"email": "ta0620050706#gmail.com",
"password": "qweQwe123!"
},
"userAddressInfo": {
"country": "United States"
},
"userPersonalInfo": {
"firstName": "test",
"lastName": "test"
}
}
}
How can extract ta0620050706#gmail.com from the following request, considering the value of the email is always dinamic?
Any help is appreciated!

If you're talking about HTTP Request sampler and the above JSON is in the "Body Data" tab like:
You can extract the email by adding a JSR223 PreProcessor and using the following code there:
vars.put('email', new groovy.json.JsonSlurper().parseText(sampler.getArguments().getArgument(0).getValue()).data.userAccountInfo.email)
It will extract the value you're looking for and store it into ${email} JMeter Variable
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

Related

JMeter - How to add data to request body in every loop?

I'm working with an API that returns response in the following format:
"products": [
{
"name": "ABC"
"id": "ABCDEFG"
"Status":Open
}
{
"name": "XYZ"
"id": "LMNOPQ"
"Status":Open
} ]
The number of products varies and so does the number of IDs generated. I need to extract all id values which I'm doing using a JSON extractor and setting the match number to -1.
I need to pass these ID values in this request:
"products": [
{
"id": "id1"
}
{
"id": "id2"
} ]
If there are 5 IDs then the request needs to contain 5 id values.
I've tried using loops but I can't figure out how to add a { "id": } to the request body on every iteration of the loop. Is there any way to simulate this?
Instead of using JSON Extractor you could do everything with JSR223 PostProcessor and extract the IDs and build the next request body in one shot.
Example code:
def ids = new groovy.json.JsonSlurper().parse(prev.getResponseData()).products.collect { product -> product.id }
def payload = [:]
def products = []
ids.each { id ->
products.add([id: id])
}
payload.put('products', products)
vars.put('payload', new groovy.json.JsonBuilder(payload).toPrettyString())
You will be able to refer generated value as ${payload} where required.
More information:
Apache Groovy: Parsing and producing JSON
Apache Groovy: What Is Groovy Used For?

Jmeter - How to get nested object in json with multiple object

I have this json:
{
"deviceId": "deviceCustom",
"moduleId": "custom",
"properties": {
"desired": {
"settings": {
"ef78c18c-2291-4d15-ae87-d89abb9b1fef": {
"name": "elements",
"version": "1.0.0",
"category": "A1"
},
"f4b04c94-4643-4b13-b10c-9a00fbf4ea27": {
"name": "tags",
"version": "2.0.0",
"category": "B1"
}
}
}
}
}
and I would like to get separately all the objects under "settings". E.g:
settings_1="f4b04c94-4643-4b13-b10c-9a00fbf4ea27":{"name":"tags","version":"2.0.0","category":"B1"}
settings_2="ef78c18c-2291-4d15-ae87-d89abb9b1fef":{"name":"elements","version":"1.0.0","category":"A1"}
settings_matchNr=2
In Jmeter I've configured a JSON Extractor with this JSON Path expression: $.properties.desired.settings but I got this result:
settings_1={"f4b04c94-4643-4b13-b10c-9a00fbf4ea27":{"name":"tags","version":"2.0.0","category":"B1"},"ef78c18c-2291-4d15-ae87-d89abb9b1fef":{"name":"elements","version":"1.0.0","category":"A1"}}
settings_matchNr=1
I've also tried to use JSR223 Post Processor with Slurper but no valid result.
Could you help me on that?
Thanks in advance.
Add JSR223 PostProcessor as a child of the request which returns the above JSON
Put the following code into "Script" area:
new groovy.json.JsonSlurper().parse(prev.getResponseData()).properties.desired.settings.entrySet().eachWithIndex { entry, index ->
def setting = [:]
setting.put(entry.getKey(), entry.getValue())
vars.put('setting_' + (index + 1), new groovy.json.JsonBuilder(setting).toPrettyString())
}
That's it, you will be able to refer the extracted JSON Objects as ${setting_1} and ${setting_2}
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

fetch the dynamic value from json response using regex or json extractor in Jmeter

From the following JSON response, want to fetch the value ref_id of both the type (now here the catch is the type place is dynamic), how to extract it using JSON or REGEX extractor or using post processer.
So if you see there are two ref_id, one is under metaData and one is under key. Want to select the which is under the meta data
for example for "type": "recall" the ref_id is cmVjYWxsLWU1ZGY4MTZkLTE0MTctNGJhNy1hMTQzLTY1OGJmMmYxYTRjMw== and for "type": "followUp" is Zm9sbG93VXAtMzk4MGRhZDQtOGE3Mi00YmM0LWFhYWEtM2Y3ZTU3NzZlNDM0.
And the position of "type": "recall" and "type": "followUp" is not constant in the json response. So I cannot use any constant index value to fetch this two value. Tried with "id": "(.*?)","type": "recall" and "id": "(.*?)","type": "followUp". But it is failing.
{
"entities": {
"cmVjYWxsLWU1ZGY4MTZkLTE0MTctNGJhNy1hMTQzLTY1OGJmMmYxYTRjMw==": {
"ref_id": "cmVjYWxsLWU1ZGY4MTZkLTE0MTctNGJhNy1hMTQzLTY1OGJmMmYxYTRjMw==",
"type": "recall",
"metaData": {
"ref_id": "e5df816d-1417-4ba7-a143-658bf2f1a4c3",
"accountId": "ef757dba-f0d5-4464-a338-4a810e02bf47",
"patientId": "e74a1b0f-d3e0-4b78-bc4b-83687786466e",
"timelineDate": "2021-01-30T14:28:24.738Z"
}
},
"Zm9sbG93VXAtMzk4MGRhZDQtOGE3Mi00YmM0LWFhYWEtM2Y3ZTU3NzZlNDM0": {
"ref_id": "Zm9sbG93VXAtMzk4MGRhZDQtOGE3Mi00YmM0LWFhYWEtM2Y3ZTU3NzZlNDM0",
"type": "followUp",
"metaData": {
"ref_id": "3980dad4-8a72-4bc4-aaaa-3f7e5776e434",
"patientId": "e74a1b0f-d3e0-4b78-bc4b-83687786466e",
"createdAt": "2021-01-29T14:36:15.127Z",
"timelineDate": "2021-01-29T14:36:15.127Z"
}
}
},
"result": [
"cmVjYWxsLWU1ZGY4MTZkLTE0MTctNGJhNy1hMTQzLTY1OGJmMmYxYTRjMw==",
"Zm9sbG93VXAtMzk4MGRhZDQtOGE3Mi00YmM0LWFhYWEtM2Y3ZTU3NzZlNDM0"
]
}
If you need just to get these ref_id attributes values from the response it can be done using JSON JMESPath Extractor, the expression is as simple as:
entities.*.ref_id
Demo:
More information:
JMESPath Tutorial
The JMeter JSON JMESPath Extractor and Assertion: A Guide
Could you please try this, as a JSON extractor.
$..[?(#.type == "recall")].ref_id
$..[?(#.type == "followUp")].ref_id

JSON path for contains

I have the below json
{
"swagger": "2.0",
"info": {
"version": "v2",
"title": "Portfolio"
},
"host": "portfolio.com",
"schemes": [
"https"
],
"paths": {
"/v2/clients/{clientId}/assets": {
"get": {
"tags": [
"assets"
There are multiple paths I want to extract but they all start /v2/clients so what Im looking for is everything that starts with /v2/clients but none of the sub-data that sits the below, just the full paths between the ""
I am using jmeter JSON extractor and if I use $.paths it starts at this point but brings through all the mass amount of sub-data. I've tried looking around stackoverflow but can't find exactly what I am looking for. Any help appreciated
I believe the easiest would be going for JSR223 PostProcessor and Groovy language
Add JSR223 PostProcessor as a child of the request which returns above JSON
Put the following code into "Script" area:
def index = 1
new groovy.json.JsonSlurper().parse(prev.getResponseData()).paths.each { path ->
if (path.getKey().startsWith('/v2')) {
vars.put('path_' + index, path.getKey())
index++
}
}
That's it, you will have JMeter Variables like:
path_1=/v2/clients/{clientId}/assets
path_2=/v2/foo/bar
path_3=/v2/baz/qux
etc.
More information:
JsonSlurper
Apache Groovy: Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

How to iterate through the contents of a Json object

On the JSR223 assertion in Jmeter, I need to validate only the inner part of the JSON returned.
I followed this thread to get an idea on the validation.
​How can I write JSON schema validation for JMeter run in TeamCity
Basically my Jmeter sampler returns the json as follows. On my schema, the validation should be for items, service and requestId. No validation should be performed for "payload".
{
"payload": [
{
"items": [
{
"code": "487482378",
"description": "Alpha Co",
"valid": true
},
{
"code": "92901128365",
"description": "Beta Co",
"valid": true
}
],
"service": "entities",
"requestId": "d190219"
}
]
}
This is my current code in the js223 sampler:
var schemaPath = '/path/entities-schema.json'
var rawSchema = new org.json.JSONObject(new org.json.JSONTokener(org.apache.commons.io.FileUtils.readFileToString(new java.io.File(schemaPath), 'UTF-8')))
var schema = org.everit.json.schema.loader.SchemaLoader.load(rawSchema)
schema.validate(new org.json.JSONObject(prev.getResponseDataAsString()))
You can remove the "unwanted" part of the response using JSR223 PostProcessor like:
def before = prev.getResponseDataAsString()
log.info('Before: ' + before)
def response = new groovy.json.JsonSlurper().parseText(before)
def after = new groovy.json.JsonBuilder(response.payload.items).toPrettyString()
log.info('After: ' + after)
prev.setResponseData(after, 'UTF-8')
Once done you can use your JSON Schema validation approach against the new content without elements you don't need.
References:
Groovy: Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

Resources