count and write the number of extracted values in regex - jmeter

I have a json with the below section. I want to write the number of providers and write it on the result file. For example there are 10 providers in this. So my requirement is to write the number 10 in the file. How can i do it?
"providers":
[
{
"MM_logofile":"agd.svg",
"MM_isOfficialWithoutLogo":false,
"code":"AGD",
"name":"Agoda.com",
"logo":"AGD.png",
"isOfficial":false
},
{
"MM_logofile":"bks.svg",
"MM_isOfficialWithoutLogo":false,
"code":"BKS",
"name":"Booking.com",
"logo":"BKS.png",
"isOfficial":false
},
{
"MM_logofile":"pln.svg",
"MM_isOfficialWithoutLogo":false,
"code":"PLN",
"name":"Priceline.com",
"logo":"PLN.png",
"isOfficial":false
},
{
"MM_logofile":"ian.svg",
"MM_isOfficialWithoutLogo":false,
"code":"IAN",
"name":"Hotels.com",
"logo":"IAN-Other.png",
"isOfficial":false
},
{
"MM_logofile":"gar.svg",
"MM_isOfficialWithoutLogo":false,
"code":"GAR",
"name":"getaroom.com",
"logo":"GAR.png",
"isOfficial":false
},
{
"MM_logofile":"htv.svg",
"MM_isOfficialWithoutLogo":false,
"code":"HTV",
"name":"HotelTravel.com",
"logo":"HTV.png",
"isOfficial":false
},
{
"MM_logofile":"exp.svg",
"MM_isOfficialWithoutLogo":false,
"code":"EXP",
"name":"Expedia.dk",
"logo":"EXP-DK.png",
"isOfficial":false
},
{
"MM_logofile":null,
"MM_isOfficialWithoutLogo":true,
"code":"BOO",
"name":"Book with hotel",
"logo":"",
"isOfficial":true
},
{
"MM_logofile":"hcl.svg",
"MM_isOfficialWithoutLogo":false,
"code":"HCL",
"name":"HotelsClick.com",
"logo":"HCL.png",
"isOfficial":false
},
{
"MM_logofile":"vnn.svg",
"MM_isOfficialWithoutLogo":false,
"code":"VNN",
"name":"Venere.com",
"logo":"VNN.png",
"isOfficial":false
}
],

Use JSONPATH Extractor plugin (use Jmeter-plugin manager to add the plugin) and add it as a child to the sampler in which you get the JSON response.
Use the expression as follows:
$.providers[*]
and reference name as providers as shown in the below image
Once you captured the values into reference variables, later you can use the reference name as follows to get the count of matched results (i.e., number of providers) as follows:
${providers_matchNr}
To save the provider's count to a file:
Add BeanShell Post processor and add the following code:
count = vars.get("providers_matchNr");
log.info(count); // if you want to log something to jmeter.log file
// Pass true if you want to append to existing file
// If you want to overwrite, then don't pass the second argument
f = new FileOutputStream("C:\\Users\\USER_HP_2013_03\\Desktop\\New folder\\result.csv", true);
p = new PrintStream(f);
this.interpreter.setOut(p);
print(count);
f.close();
Reference image:
Reference:
Write extracted data to a file using jmeter

Related

Express that, for a given property value, a property with the same name should exist using json schema?

I'm trying to validate json files which have an element that has a property which contains a value that should exist in another part of the json. I'm using jsonschema Draft 07.
This is a simple little example that shows the scenario I'm trying to validate in my data.
{
"objects": {
"object1": {
"colorKey": "orange"
}
},
"colors": {
"orange": {
"red": "FF",
"green": "AF",
"blue": "00"
}
}
}
How can I validate that the 'value' of colorKey (in this case 'orange') actually exists as a property of the 'colors' object? The data isn't stored in arrays, just defined properties.
For official JSON Schema...
You cannot check that a key in the data is the same as a value of the data.
You cannot extract the value of data from your JSON instance to use in your JSON Schema.
That being said, ajv, the most popular validator, implements some unofficial extensions. One of which is $data.
Example taken from: https://github.com/epoberezkin/ajv#data-reference
var ajv = new Ajv({$data: true});
var schema = {
"properties": {
"smaller": {
"type": "number",
"maximum": { "$data": "1/larger" }
},
"larger": { "type": "number" }
}
};
var validData = {
smaller: 5,
larger: 7
};
ajv.validate(schema, validData); // true
This would not work for anyone else using your schemas.

How can i get avg,sum of sub attribute?

I have problem with writing average query!
In my Rethink db, i have some documents in one table like this:
document1:
{
a:{
last:3
},
b:{
last:4
},
c:{
last:6
},
}
document2:
{
a:{
last:7
},
b:{
last:9
},
c:{
last:2
},
}
document3:
{
a:{
last:5
},
b:{
last:8
},
c:{
last:4
},
}
I want to get average of last attribute in every object like this:
{
sum_a_last:15,
sum_b_last:21,
sum_c_last:12,
avg_a_last:5,
avg_b_last:7,
avg_c_last:4
}
What is the query to return this result?
I believe what you're looking for is
r.db('dbName').table('tableName').avg((doc) => doc('a')('last'));
If you're trying to dynamically look for the last member for all objects in a doc there will obviously be more work.
https://rethinkdb.com/api/javascript/avg/

Parse.com manipulate Response Object

I am trying to work Ember with Parse.com using
ember-model-parse-adapter by samharnack.
I add added a function to make multiple work search(like search engine) for which I have defined a function on cloud using Parse.Cloud.define and run from client.
The problem is the Array that my cloud response returns is not compatible with Ember Model because of two attributes they are __type and className. how can I modify the response to get response similar to that i get when I run a find query from client. i.e without __type and className
Example responses
for App.List.find() = {
"results":[
{
"text":"zzz",
"words":[
"zzz"
],
"createdAt":"2013-06-25T16:19:04.120Z",
"updatedAt":"2013-06-25T16:19:04.120Z",
"objectId":"L1X55krC8x"
}
]
}
for App.List.cloudFunction("sliptSearch",{"text" : this.get("searchText")})
{
"results":[
{
"text":"zzz",
"words":[
"zzz"
],
"createdAt":"2013-06-25T16:19:04.120Z",
"updatedAt":"2013-06-25T16:19:04.120Z",
"objectId":"L1X55krC8x",
"__type" : Object, //undesired
"className" : "Lists" //undesired
}
]
}
Thanks Vlad something like this worked for me for array
resultobj = [];
searchListQuery.find({
success: function(results) {
for( var i=0, l=results.length; i<l; i++ ) {
temp = results.pop();
resultobj.push({
text: temp.get("text"),
createdAt: temp.createdAt,
updatedAt: temp.updatedAt,
objectId: temp.id,
words: "",
hashtags: ""
});
}
In your cloud code before you make any response, create and object and extract from it the attributes/members you need and then response it. like so:
//lets say result is some Parse.User or any other Parse.Object
function(result)
{
var responseObj = {};
responseObj.name = responseObj.get("name");
responseObj.age = responseObj.get("age");
responseObj.id = responseObj.id;
response.success(responseObj);
}
on the response side you will get {"result": {"name": "jhon", "age": "26", "id": "zxc123s21"}}
Hope this would help you

How do I use this JSON to bind to a Kendo UI Grid

This JSON comes back from existing server and probably won't be changed - checks out as valid using JSONLint. Autobind does not work on this, and I can't get a grid to work with it:
[
{
"SearchResult":{
"assets":[
{
"agent":"6.1.0",
"id":1,
"model":"Gateway1",
"modelId":2,
"name":"Name",
"serialNumber":"Serial01",
},
{
"agent":"M2M",
"id":2,
"model":"Gateway1",
"modelId":3,
"name":"Name",
"serialNumber":"Serial02"
}
],
"searchCriteria":{
"paginationEnabled":false,
"rowsPerPage":-1,
"startRow":-1,
"totalAvailableRows":-1,
"alternateId":{
"#xsi.nil":"true"
},
"modelNumber":{
"#xsi.nil":"true"
},
"name":"*",
"serialNumber":{
"#xsi.nil":"true"
}
}
}
}
]
You should specify the array with data in the DataSource's schema.
Have in mind that the DataSource works with flat arrays. To display the "assets" try the following:
schema: {
data: function(rawData) {
return rawData[0].SearchResult.assets;
}
}
Here is a working example: http://jsbin.com/opocib/3/edit

Parsing Mulitlevel Javascript Objects in Grails 2.1

I am trying to send data to my controller from an ajax function that needs to have multiple levels, so something like this:
{
"lob": {
"TESTING": [
{
"name": "color",
"value": "1"
},
{
"name": "time",
"value": "2"
},
{
"name": "jeremy",
"value": "3"
},
{
"name": "fourtytwo",
"value": "4"
},
{
"name": "owl",
"value": "5"
},
{
"name": "why",
"value": "6"
},
{
"name": "derp",
"value": "7"
},
{
"name": "where",
"value": "8"
}
]
}
}
but when it sends to grails I am getting this when I print out the params
[lob[TESTING][4][value]:5,
lob[TESTING][3][name]:fourtytwo,
lob[TESTING][6][name]:derp,
lob[TESTING][5][name]:why,
lob[TESTING][3][value]:4,
lob[TESTING][1][value]:2,
lob[TESTING][2][value]:3,
lob[TESTING][5][value]:6,
lob[TESTING][1][name]:time,
lob[TESTING][0][value]:1,
lob[TESTING][6][value]:7,
lob[TESTING][0][name]:color,
lob[TESTING][7][value]:8,
lob[TESTING][4][name]:owl,
lob[TESTING][7][name]:where,
lob[TESTING][2][name]:jeremy,
action:save,
controller:LOB]
The data I am sending from JavaScript:
{
lob: {
TESTING: $form.serializeArray()
}
}
I have been reading multiple forums saying using JSON.parse or request.JSON but these solutions do not seem to be fixing my problems. I want to be able to access the data like
params.lob.testing.each{ a->
println a
}
I will be doing alot more than just that but it would be nice to be able to access the data in that fashion. I am using Grails 2.1 and Jquery 1.7.2
Actually Grails makes it very easy. I've taken your test data and ran it through the following:
import grails.converters.JSON
class LobController {
def save = {
def json = request.JSON
json.lob.TESTING.each {item->
println "Name: ${item.name} - Value: ${item.value}"
}
//render something back if you need to here
}
}
And it outputs:
Name color - Value: 1
Name time - Value: 2
Name jeremy - Value: 3
Name fourtytwo - Value: 4
Name owl - Value: 5
Name why - Value: 6
Name derp - Value: 7
Name where - Value: 8
I created a UrlMapping entry like this (you probably already have this):
"/myApi"(controller: "lob", parseRequest: true) {
action = [POST: "save"]
}
The parseRequest: true will automatically parse the incoming JSON.
I found a `serializeJSON' function that might replace the serializeArray() to format this for JSON. The following was provided by Arjen Oosterkamp on the jQuery serializeArray page:
(function( $ ){
$.fn.serializeJSON=function() {
var json = {};
jQuery.map($(this).serializeArray(), function(n, i){
json[n['name']] = n['value'];
});
return json;
};
})( jQuery );
Simply use as $('form').serializeJSON();
All credit for that function goes to Arjen Oosterkamp...

Resources