How to split a String in Beanshell in Jmeter - jmeter

I'm beginner in Jmeter. I need to split a string in Beanshell Postprocessor. My String contains :
[{name=Rose, id=2, Job=manag, AGE=45}, {name=Mary, id=3, Job=eng, AGE=67},
{name=Terese, id=4, Job=exe, AGE=20}, {name=George, id=5, Job=eng, AGE=50},
{name=Mathew, id=6, Job=cwit, AGE=30}, {name=jiss, id=7, Job=bd, AGE=49},
{name=Rose, id=8, Job=exe, AGE=36}]
How can I split each value in beanshell like:
- [Rose,2,manag,45]
- [Mary, 3, eng, 67] etc.

First of all, I doubt you should be using Beanshell at all, since JMeter 3.1 it's recommended to go for JSR223 Test Elements and Groovy language for scripting
Are you sure that the string is exactly in that format? I'm asking because it's looks like to be a JSON and if this is the case you're getting response which looks like:
[
{
"name": "Rose",
"id": 2,
"Job": "manag",
"AGE": 45
},
{
"name": "Mary",
"id": 3,
"Job": "eng",
"AGE": 67
},
{
"name": "Terese",
"id": 4,
"Job": "exe",
"AGE": 20
},
{
"name": "George",
"id": 5,
"Job": "eng",
"AGE": 50
},
{
"name": "Mathew",
"id": 6,
"Job": "cwit",
"AGE": 30
},
{
"name": "jiss",
"id": 7,
"Job": "bd",
"AGE": 49
},
{
"name": "Rose",
"id": 8,
"Job": "exe",
"AGE": 36
}
]
You can use JSR223 PostProcessor and the following Groovy code:
new groovy.json.JsonSlurper().parse(prev.getResponseData()).each { entry ->
log.info(entry as String)
}
Demo:
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

Related

Turn a JSON array of key/value pairs into object properties

I'm trying to use JSONata to convert arrays of "key/value" objects into properties of the parent object. My input looks like this:
[
{
"city": "Ottawa",
"properties": [
{
"name": "population",
"value": 37
},
{
"name": "postalCode",
"value": 10001
},
{
"name": "founded",
"value": 1826
}
]
},
{
"city": "Toronto",
"properties": [
{
"name": "population",
"value": 54
},
{
"name": "postalCode",
"value": 10002
}
]
}
]
I'm struggling to generate the output I need, I've seen examples that reference explicit elements, like in this answer, but I need the properties to be converted "dynamically" since I don't know them in advance. I think I need something like this, but I'm missing some particular function:
$[].{
"city": city,
properties.name: properties.value
}
This is the output I need to generate:
[
{
"city": "Ottawa",
"population": 37,
"postalCode": 10001,
"founded": 1826
},
{
"city": "Toronto",
"population": 54,
"postalCode": 10002
}
]
The properties arrays don't always contain the same keys, but the city attributes are always present.
You can use the reduce operator, as described in the Grouping docs here:
$[].(
$city := city;
properties{ "city": $city, name: value }
)
You can play with it live: https://stedi.link/uUANwtE
Please try this expression.
$[].{
"city": $.city,
$.properties[0].name: $.properties[0].value,
$.properties[1].name: $.properties[1].value,
$.properties[2].name: $.properties[2].value,
$.properties[3].name: $.properties[3].value
}
https://try.jsonata.org/s1Ea4kUvo

JMETER - response assertion -> Response was null

I have checked several options how to validate response and it still not works in my case.
As you can see (Debug sampler), for last_name regex works and I receive some values, but if I want to reuse this response as "Response assertion", test failed, because I receive "Response was null".
JSON Extractor for last_name
Response assertion
Test failed
Also, I would like to count objects in "data", so I used http://jsonpath.herokuapp.com/?path=$..book[0,1] to make sure, the JSON file is correct. But "Debug sampler" shows me nothing :( so test failed again
Count of objects
Result
Any idea how to solve this problem please ?
JSON:
{
"page": 2,
"per_page": 6,
"total": 12,
"total_pages": 2,
"data": [
{
"id": 7,
"email": "michael.lawson#reqres.in",
"first_name": "Michael",
"last_name": "Lawson",
"avatar": "https://reqres.in/img/faces/7-image.jpg"
},
{
"id": 8,
"email": "lindsay.ferguson#reqres.in",
"first_name": "Lindsay",
"last_name": "Ferguson",
"avatar": "https://reqres.in/img/faces/8-image.jpg"
},
{
"id": 9,
"email": "tobias.funke#reqres.in",
"first_name": "Tobias",
"last_name": "Funke",
"avatar": "https://reqres.in/img/faces/9-image.jpg"
},
{
"id": 10,
"email": "byron.fields#reqres.in",
"first_name": "Byron",
"last_name": "Fields",
"avatar": "https://reqres.in/img/faces/10-image.jpg"
},
{
"id": 11,
"email": "george.edwards#reqres.in",
"first_name": "George",
"last_name": "Edwards",
"avatar": "https://reqres.in/img/faces/11-image.jpg"
},
{
"id": 12,
"email": "rachel.howell#reqres.in",
"first_name": "Rachel",
"last_name": "Howell",
"avatar": "https://reqres.in/img/faces/12-image.jpg"
}
],
You need to tick Compute concatenation var in JSON Extractor, otherwise you will get 2 separate JMeter Variables:
lastName_1=Lawson
lastName_2=Ferguson
In addition to point 1 you need to use lastName_ALL in the Response Assertion
You need to switch from Matches to Substring in "Pattern Matching Rules" of the Response assertion
Demo:
More information: How to Use the JSON Extractor For Testing

Any limitation on ORDS post API data volume?

I am trying to send a POST request of huge block of JSON to ORDS but always getting 500 - Internal server error.
If I reduce the value to around 2000 the POST API works successfully. I am using a CLOB variable to handle at JSON data inside PL/SQL proc.
What could be wrong ?
[
{
"id": 1382,
"name": "PROD_4",
"description": "Prod_Smart_Name",
"comments": "",
"entity": {
"id": 14,
"entity_name": "data_code"
},
"is_active": 1,
"is_editable": 1,
"is_visible": 1,
"is_used": 4,
"version": 4,
"is_deleted": 0,
"guid": "99044d05f0c74df1ad10c8a6a7b035bc",
"created_by": "swankhad",
"created_date": "04-OCT-17 11.24.08.569000 AM",
"last_modified_by": "runigawa",
"last_modified_date": "25-JUN-19 09.32.54.000000 AM",
"value": [
"23594333089",
"496151500740",
"881996810051",
"5358843633",
"213671772739",
"5348779195",
"5353301001",
"2482690600",
"23599643375",
"23590876041",
"5351247103",
"5358844706",
"23590871124",
"61296679111",
"23590871127",
"23590871128",
"23599103268",
"4930889250",
"213672200260",
"23590876040",
"213672229060",
"53589209680",
"23590876043",
.....
]
}]

Jmeter : How to extract first element from json array

I am trying to extract first element from a json array. Below mentioned is json array
[
{
"cohortDefinition": {
"Key": 1151,
"id": 1798,
"srcId": "3526",
"pcKey": -1,
"userName": "CHROME_USER",
"name": "JMeter2017-01-06-1483749546167",
"Type": "SUBJECT",
"tool": "CB",
"count": 32757,
"extractionStatus": "",
"dateCreated": "2017-05-10T17:48:45Z"
},
"datasource": {
"id": 2,
"name": "health",
"subjectCount": 116352
},
"project": {
"id": 747,
"name": "Jmeter Project"
}
},
{
"cohortDefinition": {
"Key": 1150,
"id": 1796,
"srcId": "3525",
"pcKey": -1,
"userName": "CHROME_USER",
"name": "JMeter2016-10-27-1477620919644",
"Type": "SUBJECT",
"tool": "CB",
"count": 32757,
"extractionStatus": "",
"dateCreated": "2017-05-10T16:57:11Z"
},
"datasource": {
"id": 2,
"name": "health",
"subjectCount": 116352
},
"project": {
"id": 747,
"name": "Jmeter Project"
}
}
]
From above json i would like to extract first value ie. srcId": "3526".
I tried doing following expression in Jmeter extractor
$..cohortDefinition.srcId[1]
However it is not working. If anyone know how to do this please do let me know.
After JMeter 3.0, you can use JSON Extractor, see:
https://stackoverflow.com/a/47043204/460802
Before JMeter 3.0:
Please follow the below steps to retrieve srcId.
Add a JSON Path Extractor to your request and configure below values.
Destination Variable Name - myVar
JSON Path Expression - $..cohortDefinition.srcId - this will extract all the srcIDs from the JSON.
Default Value - Not Found or Err
Add a Debug Sampler and View Results Tree to your test plan.
Save it and execute.
In Debug Sampler, you can view all the srcId as shown below.
You can now use myVar_1 and myVar_2 in your test plan
using ${myVar_1} ${myVar_2}
No need for Plugin, JMeter has a JSON Extractor that will provide this feature:
Notice:
JSON Path Expression is: $..cohortDefinition.srcId
Match No : 1

Configuring line charts with remote data in Kendo UI

I am looking to render a line chart using Kendo UI. http://demos.telerik.com/kendo-ui/line-charts/remote-data-binding
It expects the json data to be directly an array like in the format (from their example):
[
{
"date": "12/30/2011",
"close": 405,
"volume": 6414369,
"open": 403.51,
"high": 406.28,
"low": 403.49,
"symbol": "2. AAPL"
},
{
"date": "11/30/2011",
"close": 382.2,
"volume": 14464710,
"open": 381.29,
"high": 382.276,
"low": 378.3,
"symbol": "2. AAPL"
}
]
However, I have a URL that returns the data in the following format. Note the extra object 'ranks' at the beginning which has the array:
{
"ranks": [
{
"id": 2,
"rank": 3,
"rankdate": "2015-05-17T00:00:00+0000",
"student": {
"id": 203,
"name": "Student1",
"currentRank": 3,
"LastVerified": "2015-05-17T22:30:00+0000"
}
},
{
"id": 1,
"rank": 4,
"rankdate": "2015-05-16T00:00:00+0000",
"student": {
"id": 203,
"name": "Student1",
"currentRank": 3,
"LastVerified": "2015-05-17T22:30:00+0000"
}
}
]
}
I was wondering if there was a way to have the datasource look inside "ranks" for the array instead of expecting it directly.
Found it. One can customize the schema in Kendo for the datasource using:
schema: {
data: "ranks"
},

Resources