we are using Play framework 2.3.4. From one of the APIs we make a web service call to a third party service - structure of the returned response is dynamic and may change. Only sub-structure that's static within the response JSON is a particular element and nesting inside it. for e.g.
{
"response":
{
"someElement1": "",
"element2": true,
"buckets": [
{
"key": "keyvalue",
"docCount": 10,
"somethingElse": {
"buckets": [
{
"key": "keyvalue1",
"docCount": 5,
"somethingElseChild": {
"buckets": [
{
"key": "Tan",
"docCount": 1
}
]
}
},
{
"key": "keyvalue2",
"docCount": 3,
"somethingElseChild": {
"buckets": [
{
"key": "Ban",
"docCount": 6
}
]
}
}
]
}
}
]
}
}
we don't know how the response structure is going to look like but ONLY thing we know is that there will be "buckets" nested elements somewhere in the response and as you can see there are other nested "buckets" inside a top level "buckets" element. also please note that structure inside buckets
array is also not clear and if there will be another sub bucket it's definite that sub bucket must be somewhere inside parent bucket
- so that pattern is consistent.
what's the best way to parse such recursive structure and populate following Bucket
class recursively?
case class Bucket(key:String,docCount, subBuckets: List[Bucket] )
First I was thinking to
val json = Json.parse(serviveResponse)
val buckets = (json \ "response" \\ "buckets")
but that will not bring bring buckets
recursively and not right way to traverse.
Any ideas?
To make a
Reads[T]
for a recursive typeT
, you have tolazy val
,lazyRead
where you need recursive parsing,Reads[T]
object or its derivative.Of course you have to know what paths exactly the
buckets
element may appear at, and also account for it missing from any of those. You can useorElse
to try several paths.For your definition of
Bucket
, theReads
may look like this:You can simplify it a bit by extracting the common part to a function, e.g.:
This example doesn't account for possible merging of several
buckets
arrays at different paths inside a single bucket. So if you need that functionality, I believe you'd have to define and use aplay.api.libs.functional.Monoid
instance forReads[List[T]]
, or somehow combine the existing monoid instances forJsArray
.