Parse a JSON response and map it.

Hi community,

I’ld like to ask if there’s anyway to parse a JSONResponse as objects using Scala. The question might be vague (sorry), I’ll explain myslef.

The service returns me a JSON array of objects simular to this one.

`

[
{
“id”:“5a6a06ac2edb500008bf48ed”,
“body”:“I am posting something dumb just to test tthe thing”,
“publishDate”:“2018-01-25T17:32:44.721+01:00”,
“type”:“POST_CUSTOMIZED”,
“employee”:null,
“registrationNumber”:“03”,
“comments”:[

],
“likes”:null,
“notes”:null,
“population”:null,
“componentCode”:null,
“collectionComponent”:null
}
]

`

I’ld like to loop through those objects so I wrote this code :

`

.exec(http(“Getting the latest five posts”)
.get("/timeline/api/posts/all?sort=publishDate,desc&page=0&size=5")
.headers(headers_http_authenticated)
.check(jsonPath("$…*").findAll.saveAs(“recentFivePosts”))).exitHereIfFailed
.foreach("${recentFivePosts}", “post”){
exec{
session =>
val postType = JsonPath.query("$.type", session(“post”).as[String])
println("post : “+session(“post”).as[String]+”\ntype : "+postType)
session
}
}

`

The problem is that the variable I saved in the check part (recentFivePosts) is going to be parsed attribute by attribute (and not as full object). So when I JsonPath query the post with “$.type” it returns an empty iterator (as it should)

I could’ve directly used “$…type” and save it from the start, but I need another attribute from the same object to use as a parameter of a http request if the type is equal to “X”.

Hope I made myself clear.
Thank you!

If I understood this correctly, you want to use fetch data and then make post calls calls using that data, if so you need to create a custom feeder where you use external Http client such as scalaj, then parse Json using external library such as Play-Json and then build up the feeder as IndexedSeq[Map[String, String]]