I have this JSON object :
{"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","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}]}
Which if prettified, render this :
{
"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",
"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
}
]
}
I've got this full JSON Array from a div inside of an HTML by writing this :
.check(css("section.ht-declarations-tab-content-container>div#DATA--DECL-DATA").saveAs("jsonObj"))
And then to render the result I wrote this :
.exec { session => println("json = " + session("jsonObj").as[String]); session }.exitHereIfFailed
However as explained, I've got the full JSON array.
How to do to just get the first ID element of the first object? So basically : 00000000031000000067
This can be done in a few steps.
jsonPath
and work with jsonFor change - use transformResponse
where extract json string and remove break lines and set as new response body
http("...")
.get("...")
.transformResponse { (response, _) =>
val json = response.body.string match {
case s"""<div id="DATA--DECL-DATA">${json}</div>""" => json.replaceAll("\n", "")
}
response.copy(body = new StringResponseBody(json, response.body.charset))
}
.check(jsonPath("$...").find.saveAs("..."))