I asked a question yesterday about creating a dynamic request body with pre-processors on JMeter. Thanks to Dmitri T, I almost got what I wanted.
Old question here: JMeter Creating a Pre-Processor that will generate a request body based on a user defined variable
But right now, I cannot get dynamic values from my CSV file into the request body.
MY CSV File Example Data:
Reading CSV successfully finished, 20 records found:
${id} = 1000011
${email} = a1000011#smartmessage.com
------------
${id} = 1000012
${email} = a1000012#smartmessage.com
------------
${id} = 1000013
${email} = a1000013#smartmessage.com
My Pre-Processor that generates request body:
import groovy.json.JsonBuilder;
def payload = [:]
def X = 10 // read user input here somehow
payload.put('message_job_id', '28b0a005-9ef1-475c-b33c-ade900f19e4c')
payload.put('campaign_group', 'Entegrasyon')
payload.put('template_id', 'cf585c8c-c675-40d2-b88a-ade900c898d5')
def recipient_list = []
1.upto(X, { x ->
def recipient = [:]
recipient.put('customer_id', vars.get('id') + x)
def target = [:]
def address = ['address': vars.get('email' + x)]
target.put('target', address)
recipient.put('target', target)
recipient_list.add(recipient)
})
payload.put('recipient_list', recipient_list)
vars.put('payload', new groovy.json.JsonBuilder(payload).toPrettyString())
My Generated Request Body in View Results Tree:
{
"message_job_id": "28b0a005-9ef1-475c-b33c-ade900f19e4c",
"campaign_group": "Entegrasyon",
"template_id": "cf585c8c-c675-40d2-b88a-ade900c898d5",
"recipient_list": [
{
"customer_id": "10000111",
"target": {
"target": {
"address": null
}
}
},
{
"customer_id": "10000112",
"target": {
"target": {
"address": null
}
}
},
{
"customer_id": "10000113",
"target": {
"target": {
"address": null
}
}
},
{
"customer_id": "10000114",
"target": {
"target": {
"address": null
}
}
},
{
"customer_id": "10000115",
"target": {
"target": {
"address": null
}
}
},
{
"customer_id": "10000116",
"target": {
"target": {
"address": null
}
}
},
{
"customer_id": "10000117",
"target": {
"target": {
"address": null
}
}
},
{
"customer_id": "10000118",
"target": {
"target": {
"address": null
}
}
},
{
"customer_id": "10000119",
"target": {
"target": {
"address": null
}
}
},
{
"customer_id": "100001110",
"target": {
"target": {
"address": null
}
}
}
]
}
As you can see, address key has always null values, but I want to fill this key with email variables in CSV file.
If you need to build a request body from CSV file and send data from multiple lines in "one shot" - you won't be able to achieve this using CSV Data Set Config and derivatives because CSV Data Set Config reads next line for each iteration of the each user.
If you need to read multiple lines within the bounds of a single iteration - you will either need to switch to __CSVRead() function or read the data from the CSV file directly in Groovy.
Related
hope you all are doing well.
I need to create a complex request body. My api call will take different email addresses from a CSV file and send a email notification to the listed email addresses in the request body. But the number of the email addresses will change based on the input provided by the user. My requirements are like this:
CSV data that contains different email infos. (I have that)
The number of email addresses in the request body will be between 10 - 1000 (I thought about using a user defined variable for that)
All the email addressed will be listed in the request body.
An incremental id will be assigned to all email addresses.
So the structure of my request body should be like this:
"message_job_id": "28b0a005-9ef1-475c-b33c-ade900f19e4c",
"campaign_group": "Entegrasyon",
"template_id": "cf585c8c-c675-40d2-b88a-ade900c898d5",
"recipient_list": [
{
"customer_id":"${target_id1}",
"target":{ "address": "${email1}" }
},
{
"customer_id":"${target_id2}",
"target":{ "address": "${email2}" }
},
{
"customer_id":"${target_id3}",
"target":{ "address": "${email3}" }
},
{
"customer_id":"${target_id4}",
"target":{ "address": "${email4}" }
},
..........
{
"customer_id":"${target_idX}",
"target":{ "address": "${emailX}" }
}
X will be defined by the user input.
Any idea how can I do that?
Thanks...
My CSV sample:
Reading CSV successfully finished, 20 records found:
${id} = 1000011
${email} = a1000011#smartmessage.com
------------
${id} = 1000012
${email} = a1000012#smartmessage.com
------------
${id} = 1000013
${email} = a1000013#smartmessage.com
My output payload:
"message_job_id": "28b0a005-9ef1-475c-b33c-ade900f19e4c",
"campaign_group": "Entegrasyon",
"template_id": "cf585c8c-c675-40d2-b88a-ade900c898d5",
"recipient_list": [
{
"customer_id": null,
"target": {
"target": {
"address": null
}
}
},
{
"customer_id": null,
"target": {
"target": {
"address": null
}
}
},
{
"customer_id": null,
"target": {
"target": {
"address": null
}
}
},
{
"customer_id": null,
"target": {
"target": {
"address": null
}
}
},
{
"customer_id": null,
"target": {
"target": {
"address": null
}
}
},
{
"customer_id": null,
"target": {
"target": {
"address": null
}
}
},
{
"customer_id": null,
"target": {
"target": {
"address": null
}
}
},
{
"customer_id": null,
"target": {
"target": {
"address": null
}
}
},
{
"customer_id": null,
"target": {
"target": {
"address": null
}
}
},
{
"customer_id": null,
"target": {
"target": {
"address": null
}
}
}
]
}
Most probably you're looking for JsonBuilder class
Example code would be something like:
def payload = [:]
def X = 1 // read user input here somehow
payload.put('message_job_id', '28b0a005-9ef1-475c-b33c-ade900f19e4c')
payload.put('campaign_group', 'Entegrasyon')
payload.put('template_id', 'cf585c8c-c675-40d2-b88a-ade900c898d5')
def recipient_list = []
1.upto(X, { x ->
def recipient = [:]
recipient.put('customer_id', vars.get('target_id' + x))
def target = [:]
def address = ['address': vars.get('email' + x)]
target.put('target', address)
recipient.put('target', target)
recipient_list.add(recipient)
})
payload.put('recipient_list', recipient_list)
vars.put('payload', new groovy.json.JsonBuilder(payload).toPrettyString())
You should be able to refer generated request as ${payload} where required.
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It
Having array as:
[France, Switzerland, Croatia, Spain, Brondby, Sonderjyske, CFR Cluj, Ujpest FC, Young Boys , Xamax, Bedford Town, Biggleswade Town]
Having JSON fetched from API call as (NOT full JSON provided)
{
"payload": [
{
"id": 104,
"externalId": "e5b93978-0960-4c01-a201-b15c9e6afb49",
"shortName": "FRA",
"name": "France",
"countryCode": "en",
"leagueId": 1,
"logoUrl": "http://www.example.com/logo.png"
},
{
"id": 110,
"externalId": "7f1f76c1-483c-4c14-b5f9-61e58a5d9328",
"shortName": "Swi",
"name": "Switzerland",
"countryCode": "en",
"leagueId": 1,
"logoUrl": "http://www.example.com/logo.png"
},
{
"id": 2189,
"externalId": "8d23300c-85f4-41b4-b0f1-65e5948f2c80",
"shortName": "Cro",
"name": "Croatia",
"countryCode": "en",
"leagueId": 1,
"logoUrl": "http://www.sdfgsdf.com/image.png"
}
],
"errorCode": 0,
"errorTimeStamp": null,
"errorMessage": null,
"hasError": false
}
For every single member of the array, I need to get its ID's.
I know how match for signle member using JSON path
But, how to fetch all the ID's based on the particullar array?
Given that you have this "array" in a JMeter Variable called array you can get all the IDs by adding a JSR223 PostProcessor and using the following Groovy code in the "Script" area:
vars.get('array').replace('[', '').replace(']', '').tokenize(',').each { country ->
def result = new groovy.json.JsonSlurper().parse(prev.getResponseData()).payload.find { entry -> entry.name == country.trim() }
if (result != null) {
log.info('Country: ' + country.trim() + ', id: ' + result.id)
}
}
Demo:
More information:
JsonSlurper
Apache Groovy - Parsing and producing JSON
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
I'm getting a JSON response back from an API, however the response has several key parameters all called 'jacket' with different values. I am able to parse out the first key but I don't get the rest of the values. Here is some of the code, I might be approaching this the wrong way:
parsed_list = JSON.parse(get_response.body)
orig = parsed_list["_links"]["stuff"]["orig"]
serv = parsed_list["_links"]["stuff"]["serv"]
puts orig.first["jacket"]
puts serv.first["jacket"]
=> 123456789
=> 987654321
This is what the JSON response looks like before I parse it out and set it "parsed_list"
"_links": {
"self": {
"href": "url"
},
"stuff": {
"href": "url",
"orig": [
{
"jacket": "123456789",
"Id": "x",
"selected": true,
}
],
"serv": [
{
"jacket": "987654321",
"Id": "xx",
"selected": false,
},
{
"jacket": "0000000001",
"Id": "xx",
"selected": false,
},
{
"jacket": "1111111110",
"Id": "xx",
"selected": false,
}
]
}
}
}
I need to be able to extract all of the "jacket" values.
The data's right there, you just need to get it:
serv.collect do |entry|
entry['jacket']
end
I took suggestions from here and tried to create a class with objects for the parts I need to parse from my json feed. I then tried to deserialize it before mapping the field contents to my objects but the deserialization returns nothing.
Here is the json feed content:
"data": [
{
"id": "17xxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxx",
"from": {
"name": "Lxxxxxx",
"category": "Sports league",
"id": "17xxxxxxxxxxxxx"
},
"picture": "http://external.ak.fbcdn.net/safe_image.php?d=AQB4GscSy-2RHY_0&w=130&h=130&url=http\u00253A\u00252F\u00252Fwww.ligabbva.com\u00252Fquiz\u00252Farchivos\u00252Fbenzema-quiz-facebook.png",
"link": "http://www.xxxxxva.com/quiz/index.php?qid=34",
"source": "http://www.lxxxxva.com/modulos/redirectQuiz.php?name=benzema&q=34&time=1312827103",
"name": "DEMUESTRA CU\u00c1NTO SABES SOBRE... BENZEMA",
"caption": "www.xxxxxva.com",
"description": "Demuestra cu\u00e1nto sabes sobre Karim Benzema, delantero del Real Madrid.",
"icon": "http://static.ak.fbcdn.net/rsrc.php/v1/yj/r/v2OnaTyTQZE.gif",
"type": "video",
"created_time": "2011-08-08T18:11:54+0000",
"updated_time": "2011-08-08T18:11:54+0000",
"likes": {
"data": [
{
"name": "Jhona Arancibia",
"id": "100000851276736"
},
{
"name": "Luis To\u00f1o",
"id": "100000735350531"
},
{
"name": "Manuel Raul Guerrero Cumbicos",
"id": "100001485973224"
},
{
"name": "Emmanuel Gutierrez",
"id": "100000995038988"
}
],
"count": 127
},
"comments": {
"count": 33
}
},
{
"id": "17xxxxxxxxxxxxxxxx_xxxxxxxxxxxxx",
"from": {
"name"
and here is the code I am trying. I tried to also use the jsontextreader and loop through each tokentype and check each one and then read the value but this looks like it will turn out to be tedious.
....
//fetch the content
responsedata = reader.readtoend()
......
dim pp as facedata = JsonConvert.DeserializeObject(of faceData)(responsedata)
console.writeline(pp.id.tostring)
and the class objects
Public Class faceData
Public Property id() as string
Get
Return f_id
End Get
Set(ByVal value as string)
f_id =value
End Set
End Property
Private f_id as string
......
any response appreciated.
Does anyone have links to full code where it actually works using vb.net.