Given a parsed (via json4s) json string e.g. the following org.json4s.JValue:
val data: JValue = parse("""{
"important_field" : "info",
"some_junk" : 12345,
"interesting_stuff" : {
"pi" : 3.14,
"e" : 2.72
}
}""")
I can selectively extract the information I need:
case class Data(important_field: String)
val extracted: Data = data.extract[Data]
giving extracted: Data = Data(info). Further, I can extract information from a nested json object with another case class:
case class Stuff(pi: Double)
case class Data(important_field: String, interesting_stuff: Stuff)
val extracted: Data = data.extract[Data]
which gives me extracted: Data = Data(info,Stuff(3.14)). But now, if I want to access fields in the nested object, I need to use Stuff, my inner case class:
val pi: Double = extracted.interesting_stuff.pi
I'd prefer to drop the nesting, so Data is flat, i.e. I want to access the (originally) nested fields like this:
val pi: Double = extracted.pi
In this trivial case, using the nested class is not that painful, but when there are multiple levels of nesting it's frustrating to have to maintain that nesting in my object. Is there a typical approach for dealing with this? Some fancy Scala trick?