Gatling - extract data from JSON array response - arrays

If I have a response of this kind:
{
"A": 2,
"B": [
{
"CCC": "abcde",
"DDD": {
"EEE": 11,
"FFF": 22
}
},
{
"CCC": "fghij",
"DDD": {
"EEE": 111,
"FFF": 222
}
}
]
}
how can I get all the values CCC in a list or otherwise?
If I use:
.check(jsonPath("$..CCC").saveAs("VARIABLE"))
I only get the first CCC ("abcde"). Doing it via CCC[*] throws an error.

I think, you should have to add findAll in check statement.
for example:
.check(jsonPath("$..[*].CCC").findAll.saveAs("VARIABLE"))
And please define your error.
Cheers,
Peekay

Related

MongoDB Add Field in nested array by other field

Hello i have simple collection:
{
_id: 1,
books: [
{ bookId: 55, c: 5},
{ bookId: 66, c: 6},
{ bookId: 77, c: 7},
]
}
How i can add new field by calulate other field?
here i add field “Z” to current found object in nested array by it calculate field “C”
updateOne(
{
_id : 1,
'books.bookId' : 66
} ,
{
[
{ $addFields: { "books.$.z" : { "$sum" : ["$books.$.c", 1] } } }
]
}
Expected result:
{
_id: 1,
books: [
{ bookId: 55, c: 5},
{ bookId: 66, c: 6, z:7},
{ bookId: 77, c: 7},
]
}
I think there is a short entry (possibly using new $getField ?!), I think mango is still able to combine ‘position operator $’ + (‘varible operator reference by prefix $’ or ‘combine with $getField’) how i try in my sample
Use the aggregation pipeline in the update method to leverage the operators $map, $mergeObjects and $cond to achieve the desired results:
.updateOne(
// The query will find docs where *at least* one bookId in the
// books array equals 66 -- but remember it does not pinpoint
// the location in the array! We will tackle that in the update
// pipeline to follow...
{ _id: 1, 'books.bookId': 66 },
[
{ $set: {
books: {
$map: {
input: '$books',
in: {
$mergeObjects: [
'$$this',
{
$cond: [
{ $eq: ['$$this.bookId', 66] }, // IF books.bookId == 66
{ z: { $sum: ['$$this.c', 1] } }, // THEN merge a new field 'z' with calced value
null // ELSE merge null (a noop)
]
}
]
}
}
}
} }
]
)

In Gatling, how to iterate json array in .check to validate all values

Sample Json Response is
{
"data": {
"types": [
{
"attributes": [
{
"body": {
"id": 123
}
},
{
"body": {
"id": 456
}
}
]
},
{
"attributes": [
{
"body": {
"id": 456
}
},
{
"body": {
"id": 123
}
}
]
}
]
}
}
I want to validate that all id's are 123, if values are other than 123 Gatling test should fail.
I have tried below code it's failing in assertion but gatling report is showing passed. As I guess assert is a juint assert and not gatling assert.
val testSC1 = scenario("Sample-test")
.exec(http("Test-Post")
.post("dataservice/v1/test")
.header("Authorization", getToken)
.body(StringBody(getPayload))
.asJSON
.check(jsonPath("$.data.types[*].attributes[*].body.id").findAll.saveAs("getAllId"))
).
exec
{ session =>
//println(session("getAllId").as[String])
val iDs = session("getAllId").as[Seq[String]]
for (getEachId <- iDs)
{
assert(getEachId.equals("123"),"Actual: "+getEachId+"Expected : 123")
}
session
}
How do I iterate values in .check method
$.data.types[].attributes[].body.id ?
Or any other better solution will be helpful.
jsonPath("$.data.types[*].attributes[*].body.id").findAll.is(Seq("expectedValue1", "expectedValue2", "expectedValue3"))

Querying array in hazelcast jsonvalues

I am trying to search HazelcastJsonValue, data example for the same.
class A {
B[] listOfB;
}
class B {
int num;
String name;
}
'A' object is present in Hazelcast as HazelcastJsonValue and i want to create query which fetches all objects which contain B for which num = 10 and name = test
hazelcast query for array search using predicate
Predicate.equal("listOfB[any].name","test")
for above scenario query i can make using predicates
Predicate[] arrayOfPredicate = {Predicates.equal("listOfB[any].num",10)
,Predicates.equal("listOfB[any].name","test")};
Predicate p = Predicates.and(arrayOfPredicate);
System.out.println(p.toString()); // (listOfB[any].num=10 AND listOfB[any].name=test)
Example Data in hazelcast
[
{
"listOfB": [
{
"num": 10,
"name": "ab"
},
{
"num": 11,
"name": "test"
}
]
},
{
"listOfB": [
{
"num": 10,
"name": "test"
},
{
"num": 12,
"name": "xyz"
}
]
},
{
"listOfB": [
{
"num": 30,
"name": "abc"
}
]
}
]
Hazelcast query for same
(listOfB[any].num=10 AND listOfB[any].name=test)
But this is not giving desired results instead below result came
[
{
"listOfB": [
{
"num": 10,
"name": "ab"
},
{
"num": 11,
"name": "test"
}
]
},
{
"listOfB": [
{
"num": 10,
"name": "test"
},
{
"num": 12,
"name": "xyz"
}
]
}
]
Desired results are
{
"listOfB": [
{
"num": 10,
"name": "test"
},
{
"num": 12,
"name": "xyz"
}
]
}
How can i get desired results?
Both of the above should've been returned in your result set. Is this not the case? The fact that you're wanting any will return true for the above data. If you limited the filter to listOfB[0], then the 2nd will be returned but I'm sure your intention is to not limit to 1st item only.

building JSON string in angular from form

currently i have an api in which you send a json and the api response:
the json i send: {
"instructions": [
{
"A": 9,
"B": 1,
"move": "moveonto"
},
{
"A": 8,
"B": 1,
"move": "moveover"
}
],
"length": 20,
"res": "null"
}
and the api response: {
"instructions": [
{
"A": 9,
"B": 1,
"move": "moveonto"
},
{
"A": 8,
"B": 1,
"move": "moveover"
}
],
"length": 20,
"res": "Position [0] : 0Position [1] : 1 9 8Position [2] : 2Position [3] : 3Position [4] : 4Position [5] : 5Position [6] : 6Position [7] : 7Position [8] : Position [9] : Position [10] : 10Position [11] : 11Position [12] : 12Position [13] : 13Position [14] : 14Position [15] : 15Position [16] : 16Position [17] : 17Position [18] : 18Position [19] : 19"
}
im developing a very basic webpage:
when i click on the send to the server button i need to send it, the problem is i dont know how to build the json, can you help me? thx
i have slight idea:
Json = {
"instructions": [{
"A": $scope.addA,
"B": $scope.addB,
"move": $scope.addMov
}, {
"A": $scope.addA,
"B": $scope.addB,
"move": $scope.addMov
}],
"length": $scope.blockLength,
"res": null
};
and i send it :
$http.post("http://localhost:56493/api/BlocksProblem", Json)
.then(function (data) {
$scope.result = data;
}, function (response) {
$scope.result = response;
});
thank you very much for your time reading through all of it.
You dont necessarily "build" the json in the typical sense. By sending object data, angularjs with convert it to json for you.
for example. If i have a javascript variable like this:
var J = {A:1, B:2}
and send it as data in an http post, the json would look like:
{"A":"1","B":"2"}
Without seeing anything of what your server architecture looks like, you could do something like this
$scope.SendJson = function (Callback) {
$http({
url: YOURURL,
method: 'POST',
dataType: 'json',
data: {
"instructions": [
{"A": $scope.addA,"B": $scope.addB,"move": $scope.addMov},
{"A": $scope.addA,"B": $scope.addB,"move": $scope.addMov}],
"length": $scope.blockLength,
"res": null}
})
.then(function (data) {
$scope.result = data;
}, function (response) {
$scope.result = response;
});
})
Also, you dont need to quote the key value. Its implicit
With all that said, verify that the packet is constructed correctly in your developer tools.
reading through stackoverflow i found this:
How do I create JavaScript array (JSON format) dynamically?
so i build mine with:
//here i save the values of A,B and the moves, that i get from the form
var serverMove = [];
var serverA = [];
var serverB = [];
//create the json
var jsonS = {
instructions: [],
"length": $scope.blockLength,
"res": ""
};
//dynamically fill only the instrucions array part
for (var i = 0; i < serverA.length; i++) {
jsonS.instructions.push({
"A": serverA[i],
"B": serverB[i],
"move": serverMove[i]
})
}
the result json is something like :
{
"instructions": [
{
"A": 1
, "B": 3,
"move":
"moveonto"
},
{
"A": 5,
"B": 9,
"move": "pileover"
}
],
"length": 22,
"res": ""
}
i hope someone finds it useful

Regex to check if JSON contains an array

I need to be able to quickly discern whether or not a JSON data structure contains an array in it. For example the regex should return true for
{
"array": [
1,
2,
3
],
"boolean": true,
"null": null,
"number": 123,
"object": {
"a": "b",
"c": "d",
"e": "f"
},
"string": "Hello World"
}
and false for
{
"boolean": true,
"null": null,
"number": 123,
"object": {
"a": "b",
"c": "d",
"e": "f"
},
"string": "[Hello World]"
}
Any ideas?
I Would MUCH prefer if this check could be done via regex instead of traversing json, unless anyone can show me a better way.
You could format the JSON on some "canonical" from using a tool like jshon.
I do not recommend this approach.
$ cat foo.json
{"root": { "foo": 1, "bar": [2,3,4], "baz":"BAZ", "qux":[5,6, [7,8, [9, 10]]]}}
$ jshon < foo.json
{
"root": {
"foo": 1,
"bar": [
2,
3,
4
],
"baz": "BAZ",
"qux": [
5,
6,
[
7,
8,
[
9,
10
]
]
]
}
}
$ jshon < foo.json | grep '^\s*\]'
],
]
]
]
$ echo $?
0
Use this regex:
/:?\s+\[/g
And then you can simply do:
var json = "json here";
var containsArray = json.match(/:?\s+\[/g) !== null; // returns true or false
Demo: http://jsfiddle.net/pMMgb/1
Try
var a = {} // required json.
var regex = /\[(.*)\]/
regex.test(a)
After reviewing the answers, I've reconsidered, and I will go with parsing and traversing to find the answer to my question.
Regexing turned out to be unreliable, and the other options just add more convolution.
Thanks to everyone for the answers!

Resources