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
Related
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
My current test suite requires me to send some HTTP POST requests to API, some of which require specific objects to be posted via HTTP Request. I encountered some problems when trying to fetch those object from my YAML file when running Jmeter in Taurus.
I will attach part of my YAML file here for context (had to delete of change some properties for confidentiality):
jmeter:
properties:
number.of.users: 1000
rampup.period: 300
loop.count: 1
client.id: "23id"
array.of.clients: ["id1","id2","id3"]
ids: [1,2,3]
rq:
- "number": "7312sa1"
"signed": "2020-06-08T00:00:00.000+0000"
"crmClientId": "1-32D1P"
The problem is: when I try to pass string properties to my HTTP Request like that:
{
"id": 1986,
"jsonrpc": "2.0",
"method": "method",
"params":
{
${__P(rq,)}
}
}
all properties are wrapped in single quotation marks which causes request to receive error 400 in return because request after acquiring property is looking like this:
{
"id": 1986,
"jsonrpc": "2.0",
"method": "method",
"params":
{
'rq':
'number': '7312sa1'
'signed': '2020-06-08T00:00:00.000+0000'
'crmClientId': '1-32D1P'
}
}
Is there a way to pass string properties to request with double quotation marks or structure my YAML file in a way, which will construct request according to this example:
{
"id": 1986,
"jsonrpc": "2.0",
"method": "method",
"params":
{
rq:
"number": "7312sa1"
"signed": "2020-06-08T00:00:00.000+0000"
"crmClientId": "1-32D1P"
}
}
I tried using groovy replaceAll() method but it doesn't work with complex objects. My only solution as of right now is running some sort of groovy script in setUp thread and then acquire them is HTTP request via groovy jmeter function
You're sending a string representation of Python's dictionary, you need to send it as a simple string.
Check out YAML Multiline Strings and choose the most convenient option for you.
Example usage:
modules:
jmeter:
properties:
rq: >
\n"number": "7312sa1"\n
"signed": "2020-06-08T00:00:00.000+0000"\n
"crmClientId": "1-32D1P"\n
Taurus is presumably built to make testers and/or devops lives easier, it doesn't seem that it's your case, perhaps you should consider switching to JMeter without any wrappers instead?
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
I am trying to read a csv file that contains more that 500+ rows and each row will serve as request to API. Now my problem is that some of the parameters have empty string and i would like to set up a condition in case if parameter returns empty string then remove that parameter from request body upfront before hitting the API
Below is my json
{
"body": {
"Id1": "${Id1}",
"addressId": "${addressId}",
"languageCode": "${languageCode}",
"tempId": "${tempId}"
}
Now after reading csv i get following values in my request body
{
"body": {
"Id1": "1",
"addressId": "1233",
"languageCode": "E",
"tempId": ""
}
As you can see tempId has empty string. Now using bean-shell preprocessor i am trying to remove this but no luck
Object requestBody = sampler.getArguments().getArgument(0).getValue();
if (requestBody.get("tempId").equals("")){
sampler.getArguments.removeArgument("tempId");
}
when i look into result tree i don't see tempId being deleted from the request. I would appreciate any help
Avoid using Beanshell for deprecation and bad performance.
Use groovy instead with this code:
import org.apache.jmeter.config.Arguments;
def request = new groovy.json.JsonSlurper().parseText(sampler.getArguments().getArgument(0).getValue())
def newRequest = evaluate(request.inspect())
request.body.each { entry ->
if (entry.getValue().equals('')) {
newRequest.body.remove(entry.getKey())
}
}
def arguments = new Arguments();
sampler.setArguments(arguments);
sampler.addNonEncodedArgument('', new groovy.json.JsonBuilder(newRequest), '')
sampler.setPostBodyRaw(true)
See:
JsonSlurper
JsonBuilder
If you're looking to learn jmeter correctly, this book will help you.
Replace in body "tempId": "${tempId}" with ${tempIdEval} and calculate value in JSR223 script
String tempIdEval = vars.get("tempId");
vars.put("tempIdEval", (port == null ? "" : "\"tempId\": \"" + tempIdEval + "\""));
Migration to JSR223 PreProcessor+Groovy is highly recommended for performance, support of new Java features and limited maintenance of the BeanShell library.
Since you are using beanshell preprocessor, we can use like
if (vars.get("tempId")!="")
vars.put("variablename","not null");
else
vars.put("variablename","is null");
and use the "variablename" instead. You can manipulate the string as well as below.
if (${tempId}=="")
{ vars.put("json","
{
"body": {
"Id1": "${Id1}",
"addressId": "${addressId}",
"languageCode": "${languageCode}""
}
}
else
{ vars.put("json","
{
"body": {
"Id1": "${Id1}",
"addressId": "${addressId}",
"languageCode": "${languageCode}",
"tempId": "${tempId}"
}
}
Be aware that since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language
The relevant Groovy code for JSR223 PreProcessor which removes JSON attributes with empty values would be something like:
def request = new groovy.json.JsonSlurper().parseText(sampler.getArguments().getArgument(0).getValue())
def newRequest = evaluate(request.inspect())
request.body.each { entry ->
if (entry.getValue().equals('')) {
newRequest.body.remove(entry.getKey())
}
}
sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument('', new groovy.json.JsonBuilder(newRequest).toPrettyString(), '')
sampler.setPostBodyRaw(true)
More information:
Groovy: Parsing and producing JSON
Apache Groovy - Why and How You Should Use It
I want to extract : children from "name":"recordInstanceId" from the following JSON.
The output should give me "lQBfjAu....P0tk" . How do I do it using Regex Extractor?
{
"name":"recordTypeView",
"attributes":{
"xmlns":""
},
"children":["all"
]
},{
"name":"isRuleBacked",
"attributes":{
"xmlns":""
},
"children":["false"
]
},{
"name":"recordInstanceId",
"attributes":{
"xmlns":""
},
"children":["lQBfjAu....P0tk"
]
}
If you need to extract lQBfjAu....P0tk from the following response:
{
"name": "recordInstanceId",
"attributes": {
"xmlns": ""
},
"children": [
"lQBfjAu....P0tk"
]
}
It could be done with JSON Path Extractor (available via JMeter Plugins) with a simple JSON Path Expression like:
$..children[0]
It isn't recommended to use Regular Expressions for dealing with JSON data, I would recommend extracting data from JSON responses with the JSON Path Extractor.
References:
JSON Path Syntax
Plugin installation instructions and XPath to JSON Path mapping
If you have any troubles - update your question to show complete response