JMeter Json Extraction of random not null values - jmeter

I am using below JSON Extractor to extract random ItemID which is not null.
$..[ ? ( # .ItemID != null)].ItemID
It extracts random ItemID but also picks "null" value.
Can you please help me in this regard.
{
"Items" : [
{
"ItemID": null,
"deliveryId": "1",
}, {
"ItemID": "IT01",
"deliveryId": "2",
}, {
"ItemID": "IT02",
"deliveryId": "3",
}, {
"ItemID": "test",
"deliveryId": "3",
}
]
}

The relevant JsonPath query would be something like:
$.Items.[?(#.ItemID != null)].ItemID
Demo:
More information:
JsonPath Filter Operators
JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios

Related

Add a json attribute in a flow content with Jolt Transformation or alternative way in NiFi

I need to add this attribute named 'metadata' to json flow content.
The attribute 'metadata' is like:
{"startTime":1451952013663, "endTime":1453680013663, "name":"Npos19", "deleted":false}
The input is like this:
{
"id": 154299613718447,
"values": [
{
"timestamp": 1451977869683,
"value": 13.1
},
{
"timestamp": 1453949805784,
"value": 7.54
}
]
}
My goal is like:
{
"id": 154299613718447,
"values": [ {
"startTime":1451952013663,
"endTime":1453680013663,
"name":"Npos19",
"deleted":false,
"timestamp": 1451977869683,
"value": 13.1
},
{
"startTime":1451952013663,
"endTime":1453680013663,
"name":"Npos19",
"deleted":false,
"timestamp": 1453949805784,
"value": 7.54
}
]
}
I tried to use the Jolt Transformation:
{
"operation": "default",
"spec": {
// extract metadata array from json attribute and put it in a temporary array
"tempArray": "${metadata:jsonPath('$.*')}"
}
}
but it does not work. I need to extract metadata array with $.* because I do not know what keys will be present.
Is there an alternative fast way with other nifi processors to merge the attribute with flow content?
thanks in advance
It's possible with combination of two processors: EvaluateJsonPath ->ScriptedTransformRecord.
EvaluateJsonPath
Destination: flowfile-attribute
Return Type: json
values (dynamic property): $.values
ScriptedTransformRecord
Record Reader: JsonTreeReader
Record Writer: JsonRecordSetWriter
Script Language: Groovy
Script Body:
def mapMetadata = new groovy.json.JsonSlurper().parseText(attributes['metadata'])
def mapValue = new groovy.json.JsonSlurper().parseText(attributes['values'])
def values = mapValue.each { value ->
mapMetadata.each { k, v ->
value."${k}" = v
}
}
record.setValue('values', null)
record.setValue('updateValues', values)
record
Output json
[ {
"id" : 154299613718447,
"values" : null,
"updateValues" : [ {
"timestamp" : 1451977869683,
"value" : 13.1,
"startTime" : 1451952013663,
"endTime" : 1453680013663,
"name" : "Npos19",
"deleted" : false
}, {
"timestamp" : 1453949805784,
"value" : 7.54,
"startTime" : 1451952013663,
"endTime" : 1453680013663,
"name" : "Npos19",
"deleted" : false
} ]
} ]

Select random value from the response json with a validation

When I send a request to an api end point, the following json is coming in my response.
{
"entities": {
"practitioners": {
"f1d26a4b-c489-493d-bccf-7b9c8b92ecac": {
"fullAvatarUrl": null,
"id": "f1d26a4b-c489-493d-bccf-7b9c8b92ecac",
"accountId": "ef757dba-f0d5-4464-a338-4a810e02bf47",
"pmsId": "1",
"type": "Dentist",
"isActive": true,
"isHidden": false
},
"ee87642d-c9a6-4a9d-b99a-a96501f27a7b": {
"fullAvatarUrl": null,
"id": "ee87642d-c9a6-4a9d-b99a-a96501f27a7b",
"accountId": "ef757dba-f0d5-4464-a338-4a810e02bf47",
"pmsId": "2",
"type": "Hygienist",
"isActive": true,
"isHidden": false
},
"d0aeb9eb-f267-45ad-8cdf-eada1155c274": {
"fullAvatarUrl": null,
"id": "d0aeb9eb-f267-45ad-8cdf-eada1155c274",
"accountId": "ef757dba-f0d5-4464-a338-4a810e02bf47",
"pmsId": "3",
"type": "Dentist",
"isActive": true,
"isHidden": false
},
"2f641e8e-c5d6-4fdf-8fbe-f99fe837f441": {
"fullAvatarUrl": null,
"id": "2f641e8e-c5d6-4fdf-8fbe-f99fe837f441",
"accountId": "ef757dba-f0d5-4464-a338-4a810e02bf47",
"pmsId": "4",
"type": "Hygienist",
"isActive": true,
"isHidden": false
}
}
},
"result": [
"f1d26a4b-c489-493d-bccf-7b9c8b92ecac",
"ee87642d-c9a6-4a9d-b99a-a96501f27a7b",
"d0aeb9eb-f267-45ad-8cdf-eada1155c274",
"2f641e8e-c5d6-4fdf-8fbe-f99fe837f441"
]
}
problem statement: I want to select any one from the above 4 random value(want to select the practitioners id and which are sitting at the first element under practitioners) with a validation that isActive should be true and isHidden should be false.
I have tried using the JSON extractor using the expression $.entities.practitioners and match number 0
But it is not selecting the any one rather it select all.
Add JSR223 PostProcessor as a child of the request which returns this JSON
Put the following code into "Script" area:
def json = new groovy.json.JsonSlurper().parse(prev.getResponseData())
def ids = []
json.entities.practitioners.each { practitioner ->
if (practitioner.value.isActive && !practitioner.value.isHidden) {
ids.add(practitioner.key)
}
}
def randomId = ids.get(org.apache.commons.lang3.RandomUtils.nextInt(0, ids.size()))
vars.put('randomId', randomId)
That's it, you should be able to access the random ID as ${randomId} where required.
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It
I guess this approach can help you,
add a JSR223 PostProcessor to your http request and after that please add the following groovy script to the PostProcessor
Note: this is only to select the random PractitionersId for the validation just use the similar logic.
import groovy.json.*
def response = prev.responseDataAsString ;
def json = new JsonSlurper().parseText(response) ;
def sizeResultPractitioners = json.result.size();
Random rnd = new Random()
def randomResultPractitioners = rnd.nextInt(sizeResultPractitioners);
log.info("---------->"+randomResultPractitioners);
log.info("---------->"+json.result[randomResultPractitioners]);

Jmeter - JSON Extractor

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

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:

Parameterize POST body in JMeter HTTP POST

I am using Apache JMeter to run a few performance tests against RESTFUL API for an application that we have developed. I have an end point "api/create/empListJob", which basically adds one or more employee records in the MongoDB. The payload for the POST call looks like this:
{
"employeeList": [
{
"first_name": "josh",
"last_name": "don",
"age": "25",
"address": {
"street1": "xyz",
"street2": "apt-10",
"city" : "def",
"state" : "CA",
"zip" : "95055"
},
"deptType": {
"deptID": "1",
"deptName": "R&D"
}
},
{
"first_name": "mary",
"last_name": "jane",
"age": "22",
"address": {
"street1": "zzz",
"street2": "apt-15",
"city" : "yyy",
"state" : "CA",
"zip" : "95054"
},
"deptType": {
"deptID": "2",
"deptName": "HR"
}
}
]
}
As you can see, the payload takes a list of employee data, and it should have atleast one employee record. I have a requirement in which i want JMeter thread group to have 10 threads and each of these threads should make a concurrent POST to "api/create/empListJob" such that the body has 10 unique employee records, thus creating a total of 100 records. What is the best way that i could parameterize the payload?
Take a look at JMeter Functions, like:
__threadNum() - returns the number of current thread (virtual user)
__Random() - returns a random number in a given range
__RandomString() - returns a random string from specified input characters
__UUID() - returns random GUID structure
So for example if you change your JSON payload to look like:
"employeeList": [
{
"first_name": "josh-${__threadNum}",
"last_name": "don-${__threadNum}",
"age": "25",
"address": {
"street1": "xyz",
"street2": "apt-10",
"city" : "def",
"state" : "CA",
"zip" : "95055"
},
"deptType": {
"deptID": "1",
"deptName": "R&D"
}
},
{
"first_name": "mary-${__threadNum}",
"last_name": "jane-${__threadNum}",
"age": "22",
"address": {
"street1": "zzz",
"street2": "apt-15",
"city" : "yyy",
"state" : "CA",
"zip" : "95054"
},
"deptType": {
"deptID": "2",
"deptName": "HR"
}
}
]
}
JMeter will create:
- `josh-1` for 1st virtual user
- `josh-2` for 2nd virtual usre
- etc.
See Apache JMeter Functions - An Introduction to get familiarized with JMeter Functions concept.

Resources