Hello,
I am using the standard library:
`
import scala.util.parsing.json.JSON
`
to parse a chunk of JSON that looks like:
`
{“results”:[
{“columns”:[“id”],
“data”:[{“row”:[“one”],“meta”:[null]},{“row”:[“two”],“meta”:[null]}]}
],
“errors”:[]
}
`
I have an “Initialization” scenario that pulls data from the database and adds it to a csv file which gets used as a feeder by other tests.
It looked like this;
`
val scn = scenario(“Fetch Items”)
.repeat(1) {
exec {
http(“fetch Items”)
.post("/db/data/transaction/commit")
.basicAuth(“neo4j”, “my password”)
.body(StringBody(cypherQuery))
.asJSON
.check(status.is(200))
.check(validatorCheckBuilder2Check(bodyString.transform(body => {
val json:Option[Any] = JSON.parseFull(body)
val map:Map[String,Any] = json.get.asInstanceOf[Map[String, Any]]
val results:List[Any] = map(“results”).asInstanceOf[List[Any]]
val data:Option[List[Map[String, List[String]]]] = results.head.asInstanceOf[Map[String,List[Map[String, List[String]]]]].tail.get(“data”)
if(data.nonEmpty) {
data.head.foreach(d => itemsWriter.println {
d.get(“row”).head.head
})
}
})).exists)
}
}
`
I can sort-of short cut the check to:
`
.check(bodyString.transform(body => {
val data:Option[List[Map[String, List[String]]]] = JSON.parseFull(body) match {
case Some(m: Map[String, List[Map[String,List[Map[String, List[String]]]]]]) => m("results").head.get("data")
}
if (data.nonEmpty) {
data.head.foreach(d => itemsWriter.println {
d.get("row").head.head
})
}
}).exists)
`
but that still looks pretty ugly. Do you know a more elegant way to do this?
Thanks,
Max