Jmeter - JSON Extractor - jmeter

I'm trying create a json expression path that returns the id when the reference with {" id ":" 00000000000000000000000004640254 "}. I have tried with
$.[?(#.Relationship[?(#.Ref.id=='00000000000000000000000004640254')])].id
but it doesn't return data
Json message is
[
{
"id": "234567890234567890",
"Relationship": [
{
"type": "Indirect",
"Ref": {"id": "00000000000000000000000004640253_01"}
},
{
"type": "Direct",
"Ref": {"id": "00000000000000000000000004640254"}
}
],
"Specification": {"id": "Gold123AS"}
},
{
"id": "234567890234567891",
"Relationship": [
{
"type": "Indirect",
"Ref": {"id": "00000000000000000000000004640253_02"}
},
{
"type": "Direct",
"Ref": {"id": "00000000000000000000000004640253"}
}
],
"Specification": {"id": "Gold123AS"}
}
]
if someone can help me, thanks

I don't think you can do this using JSON Extractor as returning the parent node is not implemented by underlying JsonPath library
Go for JSR223 PostProcessor and Groovy language instead, the relevant code would be something like:
def json = new groovy.json.JsonSlurper().parse(prev.getResponseData())
0.upto(json.size() -1 , { idx ->
if (json.get(idx).Relationship.find { it.Ref.id.equals('00000000000000000000000004640254') } != null) {
vars.put('id', json.get(idx).id as String)
return
}
})
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

Related

How to filter any data on any filed is present or not in object in GraphQL

Below is my sample data which store in Dgraph
{
"data": {
"comp": [
{
"topologytemplate": {
"namespace": "a",
"nodetemplates": [
{
"name": "a",
},
{
"name": "b",
}
]
}
},
{
"topologytemplate": {
"namespace": "c",
"nodetemplates": [
{
"name": "a",
},
{
"name": "b",
"directives": [
"a"
]
},
]
}
},
]
},
}
I want to filter data so that as a result we get data that does not contain "directives" filed. I am want to filter data using GraphQL query?
Currently, I am trying to filter data as follows:
query {
comp(func: eq(dgraph.type,"QQQ")){
name
topologytemplate{
nodetemplates #filter (eq(nodetypename,"a")){
name
directives
}
}
}
}
Query to check that directives filed are present or not in nodetemplate?

Retrieve request data information's using JSR223 post processer in JMeter

I am using the following payload as post request to one of my test servers, and I want to retrieve the size of the payload, uniquid from the payload. I am using JSR223 post processer for this any help to get these information
Sample Payload:
POST https://test.eventgrid.azure.net/api/events
POST data:
[
{
"subject": "audit",
"id": "6aca5990-713b-47d1-be81-ed228bd81735",
"eventType": "test.audit",
"eventTime": "2020-08-31T05:02:02.462Z",
"data": {
"version": "1.0",
"application": {
"id": "PI0001",
"name": "PLMAS",
"component": {
"id": "PLMAS01",
"name": "SingleFileImporter",
"type": "LogicApp"
}
},
"audit": {
"id": "168999807c4c46af908ce7a455a5e5eb",
"timestamp": "2020-08-31T05:02:02.462Z",
"type": "input",
"entry": "File retrieved, validated and processed successfully",
"message": {
"headers": "J9SGinwTz0SSrEHrBrhMS3wquHlWu",
"payload": "00=SfsDZ0LESTLZ6VpCmIEDT5nqOPqlwUJknCSIQuAIBM8wKj",
"type": "csv",
"protocol": ""
},
"keys": [
{
"name": "file-archive-location",
"value": "Performance Test From Jmeter"
}
]
},
"context": {
"transactionId": "65174971-62d6-44da-9ecd-537b8d636464",
"messageId": "04cb206c-25dd-4385-bed7-42f770c67cb8",
"customerId": "FANSOI",
"studyId": "FANSOI1234"
}
},
"dataVersion": "1.0",
"metadataVersion": "1"
}
]
Is there any default method like sampler.getUrl() to get the request url and sampler.getArguments().getArgument(0).getValue() to get the request body.
This should do what you want:
import java.util.List;
def size = prev.getBodySizeAsLong() + prev.getHeadersSize();
List<String> list = com.jayway.jsonpath.JsonPath.read( prev.getQueryString(), "$..id");
String uniqueId = list.get(0).toString();
log.info("size:{}, uniqueId:{}", size, uniqueId);
You can use the same functions but instead of sampler go for ctx.getCurrentSampler(), something like:
def data = ctx.getCurrentSampler().getArguments().getArgument(0).getValue()
def size = data.length()
def id = new groovy.json.JsonSlurper().parseText(data)[0].id
log.info('Size: ' + size)
log.info('Id: ' + id)
Demo:
More information:
Apache Groovy - Parsing and producing JSON
Top 8 JMeter Java Classes You Should Be Using with Groovy

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

How to extract multiple json values from a Json response

I am trying to extract multiple values from a JSON response on my jmeter script. Below is sample of my response:
{
"startDate": "2018-12-10T15:36:34.400+0000",
"userId": "7211111-2fa90",
"createdBy": "TEST",
"note": {
"content": "Application Submitted "
},
"Type": "SUBMITTED"
},
"currentEventState": "CLOSED",
{
"Xxxx": "test",
"Loc": null,
"Zipcode": [],
"Locality": 82,
"Address": {
"Add": 12302,
"Add2": "place",
"Zip": {
"Phone": "home",
"Email": "test#test.com"
}
},
"state": "MD",
"Cost": "E "
},
"AppID": "cd8d98e6-c2a79",
"Status": "CLOSED",
}
I am trying to extract userid and AppID for the case if the TYPE is Submitted and Status is Closed.I tried using the Json extractor with $.[?(#.Type=="SUBMITTED")].[*].?(#.Status=="CLOSED").userid,APPID, but couldn't get the expected result. Could anyone guide me on this.
You need to use an inline predicate to combine 2 clausees and a semicolon in order to store results into 2 separate JMeter Variables.
Add JSON Extractor as a child of the request which returns above JSON
Configure it as follows:
Names of created variables: userid;appid
JSON Path Expressions: $..[?(#.Type=='SUBMITTED' && #.Status == 'CLOSED')].userId; $..[?(#.Type=='SUBMITTED' && #.Status == 'CLOSED')].AppID
Default values: NA;NA
Here is the demo of single expression working fine:
And here are extracted values reported by the Debug Sampler:

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