Regex for JMeter JSON Extractor - json

I'm using JMeter to validate some HTTPS requests, and for that, I'm using one JSON extractor and I want to extract data from the JSON response I'm getting. So from the below payload, I want to extract oper_state from any resource where service_name is equal to "Japan-1-3-12_service".
{
"resource":[
{
"id":"de04c6b1-a5a3-11ec-a02b-765e38f104a5-19",
"name":"Tokyo-1-1-10",
"service_name":"Tokyo-1-1-10_service",
"oper_state":"UP",
"type":"admin"
},
{
"id":"me05c6b1-a903-11ec-a02b-764313f104a5-19",
"name":"Japan-1-3-12",
"service_name":"Japan-1-3-12_service",
"oper_state":"UP",
"type":"admin"
},
{
"id":"5e04c691-a5a3-11ec-a02b-765e38f3q4a5-19",
"name":"France-1-1-3",
"service_name":"France-1-1-3_service",
"oper_state":"DOWN",
"type":"admin"
}
]}
I was using "$.resource[?(#.service_name=="Japan-1-3-12_service")].operational_state", but not getting any output in the variable.

Why you're using operational_state when in your JSON response it's oper_state everywhere?
Just change your JSON Path query to
$.resource[?(#.service_name=="Japan-1-3-12_service")].oper_state
and it should start working as expected
More information: JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios

Related

How I can convert JSON to payload of HTTP GET string?

I am trying to get data using GDC portal api. As it is shown in GDC API, to send an HTTP GET request, we need to have a format similar to:
https://api.gdc.cancer.gov/files?filters=%7B%22op%22%3A%22and%22%2C%22content%22%3A%5B%7B%22op%22%3A%22in%22%2C%22content%22%3A%7B%22field%22%3A%22cases.submitter_id%22%2C%22value%22%3A%5B%22TCGA-CK-4948%22%2C%22TCGA-D1-A17N%22%2C%22TCGA-4V-A9QX%22%2C%22TCGA-4V-A9QM%22%5D%7D%7D%2C%7B%22op%22%3A%22%3D%22%2C%22content%22%3A%7B%22field%22%3A%22files.data_type%22%2C%22value%22%3A%22Gene%20Expression%20Quantification%22%7D%7D%5D%7D&format=tsv&fields=file_id,file_name,cases.submitter_id,cases.case_id,data_category,data_type,cases.samples.tumor_descriptor,cases.samples.tissue_type,cases.samples.sample_type,cases.samples.submitter_id,cases.samples.sample_id,analysis.workflow_type,cases.project.project_id,cases.samples.portions.analytes.aliquots.aliquot_id,cases.samples.portions.analytes.aliquots.submitter_id&size=1000
Which is a format from json payload as:
{
"filters":{
"op":"and",
"content":[
{
"op":"in",
"content":{
"field":"cases.submitter_id",
"value":[
"TCGA-CK-4948",
"TCGA-D1-A17N",
"TCGA-4V-A9QX",
"TCGA-4V-A9QM"
]
}
},
{
"op":"=",
"content":{
"field":"files.data_type",
"value":"Gene Expression Quantification"
}
}
]
},
"format":"tsv",
"fields":"file_id,file_name,cases.submitter_id,cases.case_id,data_category,data_type,cases.samples.tumor_descriptor,cases.samples.tissue_type,cases.samples.sample_type,cases.samples.submitter_id,cases.samples.sample_id,analysis.workflow_type,cases.project.project_id,cases.samples.portions.analytes.aliquots.aliquot_id,cases.samples.portions.analytes.aliquots.submitter_id",
"size":"1000"
}
I wanna add more fields to the payload json and get the equivalent http query string format. I have tried some online convertors, like: JSON-to-String Convertor. Copying the above JSON gives me the below result:
"{\"filters\":{\"op\":\"and\",\"content\":[{\"op\":\"in\",\"content\":{\"field\":\"cases.submitter_id\",\"value\":[\"TCGA-CK-4948\",\"TCGA-D1-A17N\",\"TCGA-4V-A9QX\",\"TCGA-4V-A9QM\"]}},{\"op\":\"=\",\"content\":{\"field\":\"files.data_type\",\"value\":\"Gene Expression Quantification\"}}]},\"format\":\"tsv\",\"fields\":\"file_id,file_name,cases.submitter_id,cases.case_id,data_category,data_type,cases.samples.tumor_descriptor,cases.samples.tissue_type,cases.samples.sample_type,cases.samples.submitter_id,cases.samples.sample_id,analysis.workflow_type,cases.project.project_id,cases.samples.portions.analytes.aliquots.aliquot_id,cases.samples.portions.analytes.aliquots.submitter_id\",\"size\":\"1000\"}"
So, I tried to get data using the generated string by adding
https://api.gdc.cancer.gov/files?
to the beginning of that. But, running this, gives me a different output. So, how can I convert the JSON to exactly what is given in GDC website? Sorry, I am very new to API and their usages.

How to parse dynamic json reponse and get specific value and pass it as an input to next request

I get .json file as a response from an API and from that file I should parse and fins specific parameter and pass it as an input to the next request, how do I do that using Katalon.
If I say
response = JSON.parse("response.json");
it says it is unable to identify JSON as valid. Can someone help me out with the solution?
Your JSON is invalid, maybe it is a copy-paste issue.
The valid JSON should be
{
"responseStatusCode": "OK",
"data": {
"screenName": "employeeTimeslip",
"screenType": "Redirect",
"searchResultCount": 0,
"rows": [],
"tabs": [],
"searchParams": {
"employeeID": "000092926",
"timeslipNumber": "201900019701"
}
}
}
So, you were missing a "," between "OK" and "data" and two closing curly braces at the end of the file.
You can check JSON files for validity yourself using online JSON validators, for example, this one.
i found a way to read specific parameter from the json response file like below:
val scn = scenario("ClaimSubmission")
.exec(http("request_2")
.post("URL")
.headers(headers_2)
.body(RawFileBody("json file path"))
.check(jsonPath("$..timeslipnumber").find.saveAs("timeslipnumber")))
Timeslip number would be retrieved using : .check(jsonPath("$..timeslipnumber").find.saveAs("timeslipnumber")))

JSR223 Preprocessor give Bad request while creating json in Jmeter using CSV

I am trying to create a JSON using Groovy Json Builder in Jmeter.
Below is the code I created to get the desired JSON :
import groovy.json.JsonBuilder
def json = new JsonBuilder()
json {
"Rule":{
"__type":"AndroidSamsungDeviceRelocationRule",
"RuleId":0,
"Name":"Test",
"DeviceFamily":6,
"Targets":{
"Groups":[
{
"Id":"[{LogicalId="${logicalid1}"}]"
}
],
"Devices":[
]
},
"Priority":0,
"IsEnabled":true,
"StartDate":"/Date(${__time(,)})/",
"EndDate":null,
"Mappings":[
{
"RelocationTarget":{
"Id":"${logicalid2}",
"Name":"${__P(TargetGroups)}",
"Path":"\\'${__P(TargetGroups)}'\\",
"PathVisible":"\\'${__P(TargetGroups)}'\\"
},
"IPRange":[
source Double.parseDouble(vars.get("source"))
destination Double.parseDouble(vars.get("destination"))
]
}
],
"EnrollmentCertificateId":null,
"EnrollmentCertificateName":""
}
}
sampler.addNonEncodedArgument("",json.toPrettyString(),"")
sampler.setPostBodyRaw(true)
CSV data is as follows:
0.0.0.0,255.255.255.255
10.0.0.1,255.0.0.0
The above CSV can have multiple values from 1 to 100 IPs separated by ',' .
The code also needs to be modified as per the number of IPs rows.
The ABove CSV data needs to be there under IPRange tag and Result Should be like:
"Rule":{
"__type":"AndroidSamsungDeviceRelocationRule",
"RuleId":0,
"Name":"Test",
"DeviceFamily":6,
"Targets":{
"Groups":[
{
"Id":"[{LogicalId="${logicalid1}"}]"
}
],
"Devices":[
]
},
"Priority":0,
"IsEnabled":true,
"StartDate":"/Date(${__time(,)})/",
"EndDate":null,
"Mappings":[
{
"RelocationTarget":{
"Id":"${logicalid2}",
"Name":"${__P(TargetGroups)}",
"Path":"\\'${__P(TargetGroups)}'\\",
"PathVisible":"\\'${__P(TargetGroups)}'\\"
},
"IPRange":[
{"From":"0.0.0.0","To":"255.255.255.255"},
{"From":"10.0.0.1","To":"255.0.0.1"},
......
.....
]
}
],
"EnrollmentCertificateId":null,
"EnrollmentCertificateName":""
}
But the issue I am getting is "Bad Request" in jmeter.
I fail to see why do you need to build your request body in Groovy, it can be done purely in HTTP Request "Body Data" tab like:
Check out JMeter Parameterization - The Complete Guide for examples on using external data sources for creating data-driven JMeter tests
If you want us to help you with the setup it would be good to see the desired outcome, i.e. JSON payload you would like to generate and the code you've tried as above samples don't make any sense. Get used to check jmeter.log file - normally it contains enough information to get to the bottom of the JMeter test issue. Also I suggest reading Parsing and producing JSON article and JsonBuilder class documentation.

How to load a json file with unit ML test examples into jmeter http request payload

I have an endpoint that serves a ML model and I want to perform load testing on it. I'm using Jmeter 4.0 and its UI to construct a simple plan test. With 1 thread group that loops for a given duration and continuosly performs https requests.
How do I parse multiple test examples into the payload of a http request, one by one and in json format. These examples are contained in a json file called samples.json. The nested structure is the following:
{ "dataset": [
{"id": 1,
"in":[
{
"Feature1": 8.9
"Feature2":7.1
}],
"out": "Class1",
},
{"id": 2,
"in":[
{
"Feature1": 3.2
"Feature2":5.1
}],
"out": "Class1",
}]
}
IMPORTANT: I do not know the number of attributes a priori, so I need to retrieve them from the in key as that may change for other types of models, therefore I can't make use of harcoded jmeter variables, similar to what it's used in the CSV Config Set add-on, where they need to specify the variables names for each column of the csv file
I have no idea how you're gonna use the values from JSON in the HTTP Request sampler, however this is how you can parse your samples.json file and get in values from it in the JSR223 Sampler
new groovy.json.JsonSlurper().parse(new File('samples.json')).dataset.each { entry ->
entry.get('in').each { feature ->
feature.each { value ->
log.info(value.key + '=' + value.value)
}
}
}
The above code basically prints the keys and respective values into jmeter.log file
But you can easily amend it to store the values into JMeter Variables, write them into a CSV file, set HTTP Request sampler to use them on the fly, etc.
More information:
Groovy: Parsing and producing JSON
Apache Groovy - Why and How You Should Use It
Apache JMeter API

Building json path expression - presence of DOT in attribute name

We are working with a legacy system which gives json responses. We trying to test these json endpoints with jmeter. So, we are trying to use the json path extractor plugin for the purpose. But the structure of the json path is causing an issue in creating json path expressions.
The structure of the json which we are receiving from the server is as follows.
{
"ns9.Shopping": {
"transactionID": "XXXXXNEKIHJO7SRHN1",
"transactionStatus": "Success",
"ns9.shoppingResponseIDs": {
"ns9.owner": "1P",
"ns9.responseId": "abcdefghijklmnop"
},
"ns9.offersGroup": {"ns9.thanksGiving": [
{
"ns9.owner": "DL",
"ns9.retailOffer": [
{
"ns9.offerId": "offer1DL",
"ns9.price": 2639.08,
"ns9.currencyCode": "USD",
"ns9.taxTotal": 961.08,
"ns9.taxCode": "USD",
.........
The presence of . [DOT] in the attribute name is causing issues in my json path expression.
In short, can some one help in finding the "transactionID" from "ns9.Shopping"?
You can try to add Regular Expression Extractor within your HTTP Request element.
Put this regex:
"transactionID": "([a-zA-Z0-9]*)"
I hope this will help you.