I'm new to Gatling, and a bit struggling on this.
I have this JSON object coming from an HTML :
<div id="DATA--DECL-DATA">{"isCompany":false,"accommodations":[{"id":"00000000031000000067","isChecked":false,"name":"5 JULI 2017","addressLine1":"STRAAT 10 ","addressLine2":"1000 New York","nightsDeclared":0,"schoolNightsDeclared":0,"schoolNightsAttached":0,"taxableNights":0.0,"totalPayment":0.0,"isInProgress":false,"isLate":false,"isPayed":"false","deadline":"2021-12-31","initialAmount":0.0,"remainingAmount":0.0},{"id":"00000000031000006362","isChecked":false,"name":"BELLEVIE","addressLine1":"STRAAT 10 ","addressLine2":"1000 New York","isInProgress":false},{"id":"00000000031000006380","isChecked":false,"name":"BELLEVIE","addressLine1":"STRAAT 10 ","addressLine2":"1000 New York","isInProgress":true},{"id":"00000000031000006390","isChecked":false,"name":"BELLEVIE","addressLine1":"STRAAT 10 ","addressLine2":"1000 New York","isInProgress":true}]}</div>
Which if prettified, render this :
{
"isCompany": false,
"accommodations": [
{
"id": "00000000031000000067",
"isChecked": false,
"name": "5 JULI 2017",
"addressLine1": "STRAAT 10 ",
"addressLine2": "1000 New York",
"isInProgress": false
},
{
"id": "00000000031000006362",
"isChecked": false,
"name": "BELLEVIE",
"addressLine1": "STRAAT 10 ",
"addressLine2": "1000 New York"
"isInProgress": false
},
{
"id": "00000000031000006380",
"isChecked": false,
"name": "BELLEVIE",
"addressLine1": "STRAAT 10 ",
"addressLine2": "1000 New York",
"isInProgress": true
},
{
"id": "00000000031000006390",
"isChecked": false,
"name": "BELLEVIE",
"addressLine1": "STRAAT 10 ",
"addressLine2": "1000 New York",
"isInProgress": true
}
]
}
To get this JSON Array from that div and save it into a session variable in Gatling, I wrote this "check" :
.check(css("div#DATA--DECL-DATA").saveAs("myJsonObj"))
And then to print the result in the console once the script is executed, I wrote this :
.exec { session => println("json = " session("myJsonObj").as[String]); session }.exitHereIfFailed
This will print in the console, the full prettified JSON array that you saw above.
Now in that accommodations JSON Array we can see that there are severals ids when the "isInProgress" is false.
My question then is, how to do get the first id of an accommodation when that "isInProgress" is false ?
So : if "IsInProgress" is false => get the first accommodation id in that array.
CodePudding user response:
The following code is in Javascript, but should be easy to convert to scala.
var json = {
"isCompany": false,
"accommodations": [
{
"id": "00000000031000000067",
"isChecked": false,
"name": "5 JULI 2017",
"addressLine1": "STRAAT 10 ",
"addressLine2": "1000 New York",
"isInProgress": false
},
{
"id": "00000000031000006362",
"isChecked": false,
"name": "BELLEVIE",
"addressLine1": "STRAAT 10 ",
"addressLine2": "1000 New York",
"isInProgress": false
},
{
"id": "00000000031000006380",
"isChecked": false,
"name": "BELLEVIE",
"addressLine1": "STRAAT 10 ",
"addressLine2": "1000 New York",
"isInProgress": true
},
{
"id": "00000000031000006390",
"isChecked": false,
"name": "BELLEVIE",
"addressLine1": "STRAAT 10 ",
"addressLine2": "1000 New York",
"isInProgress": true
}
]
}
var FilteredJson = json.accommodations.filter((value) => {if (value.isInProgress === false) {return value}});
//just take the first value of FilteredJson
console.log(FilteredJson[0]);
CodePudding user response:
According to my previous answer you need check
with a json-path with condition:
.check(jsonPath("$.accommodations[?(@.isInProgress==false)].id").find.saveAs("id"))
Explanation:
@.isInProgress==false
- it's a condition for each object inside array accommodations
.
More details about json-path can be found here
Gatling's method find
returns the first occurrence. Also exist method findAll
which returns all values...