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?