JMeter Script to extract an id from Json response - jmeter

I need to extract id which as Status 1. How can I achieve it.
Example: id (abc) in below example as its status is 1
''''
{
"Documents": [
{
"id": "abc",
"Status": 1,
},
{
"id": "pgr",
"Status": 2,
},
{
"id": "abc",
"Status": 2,
}
]
}
''''
Thanks

You can do this using JSON Extractor, the relevant JSONPath query would be something like:
$..[?(#.Status == 1)].id
Demo:
More information:
JSONPath Filter Operators
JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios

Related

json extractor expression to extract field outside array

How can I grab productId from the outside array after checking for the condition. I have tried $..items[?(#.stock.available==true)].productId
{
"listings": [
{
"productId": "100-dark-hot-chocolate",
"items": [
{
"stock": {
"available": true
}
}
],
"test": null
}
]
}
This one should give you what you're looking for:
$.listings[*][?(#.items[0].stock.available == true)].productId
Demo:
More information:
JsonPath - Filter Operators
JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios

Extracting only relevant item from json blob in jmeter

I am quite new on JSR223 which I have been trying out. I have a use case when an item which are available=true only I want to grab id and ignoring out of stock item from below JSON blob. Can someone please advise?
"items": {
"104": {
"id": "104",
"stock": {
"available": true,
"lowOnStock": false,
"quantity": 11
},
},
"105": {
"id": "105",
"stock": {
"available": false,
"lowOnStock": true,
"quantity": 0
},
}
Thanks in advance.
It's recommended to avoid scripting where possible so you can go for JSON Extractor and the following JSONPath query:
$..[?(#.stock.available==true)].id
Demo:
More information: JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios

Jmeter-Regular expression extraction-how to get the value of attribute name

I have the below response from Rest service. I need do capture the value of dSecurityGroup which is "TEST" and pass it to next request. Can yu help on this
"GenericResponse": {
"Service": {
"IdcService": "CHECKIN_UNIVERSAL",
"Document": {
"Field": [
{
"name": "xIPM_APP_1_9:isSetDefault",
"value": "1"
},
{
"name": "IdcService",
"value": "CHECKIN_UNIVERSAL"
},
{
"name": "dSecurityGroup",
"value": "TEST"
},
{
"name": "xIPM_APP_1_6:rule",
"value": "IpmApp_1_Fields_Hide"
},
{
"name": "dpTriggerField",
"value": "xIdcProfile"
},
]
}
}
}
Your JSON response is invalid. check with https://jsonformatter.curiousconcept.com/ and update the correct JSON
Your response is JSON therefore it doesn't make sense to use Regular Expression Extractor.
Consider using JSON Extractor instead, it allows using JsonPath queries which provide handy way to extract the "interesting" values from JSON responses.
In your case the relevant query would be something like:
$..[?(#.name == 'dSecurityGroup')].value
Demo:
More information: API Testing With JMeter and the JSON Extractor

Can I use for loop in karate?

I am using get request for getting json response. And it is dynamic. My response looks like this.
{
"data": [
{
"id": 1,
"name": "Jack",
"gender": "male"
},
{
"id": 2,
"name": "Jill",
"gender": "female"
}
]
}
Can I use id from this response and put that id in 'for loop' to execute delete method?
Given path 'profile/delete/id'
And if I can then how?
Yes, refer the docs: https://github.com/intuit/karate#data-driven-features
* def result = call read('delete.feature') response.data
And in delete.feature you will be able to refer to the id variable directly.

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

Resources