Complex JSON generation

I am working on a complex json build that pulls data from a database and is required to build the data into a complex structure of json after a little data manipulation. I have case classes, or models, that I must follow in order for the data to be processed by the java service application. So far I have been unable to find any resource on the web that shows this complex of a data structure. Looking for a direction to start in to generate this json structure given is a small sample of the model with the parent being WatchModel:

case class WatchModel(watchId: String,
client: Client,
person: Array[Person],
deliveryChannels: Array[DeliveryChannel],
expireTs: Array[Int],
creationTs: Array[Int],
lastChangeTs: Array[Int])

case class Person(addresses: Array[Address],
names: Array[Name],
datesOfBirth: Array[DOB],
identities: Array[Identity])

case class Name(firstName: String, middleName: String, lastName: String)

Also, these are pulled using spark so I have everything pulled into datasets and all of the minor models are matched too, but I have been unable to construct the parent model.

What difficulties have you run into? Most JSON libraries provide macros or reflection to automatically serialize case classes into JSON, as long as all components are themselves serializable.

For instance, the uPickle documentation suggests:

import upickle.default.{ReadWriter => RW, macroRW}

// After that, you can begin serializing that case class.

case class Thing(myFieldA: Int, myFieldB: String)
object Thing{
  implicit val rw: RW[Thing] = macroRW
}
case class Big(i: Int, b: Boolean, str: String, c: Char, t: Thing)
object Big{
  implicit val rw: RW[Big] = macroRW
}

used:

write(Big(1, true, "lol", 'Z', Thing(7, "")))

Output:

{"i":1,"b":true,"str":"lol","c":"Z","t":{"myFieldA":7,"myFieldB":""}}

Most of them nest arbitrarily deep, so there’s no difference between “this complex” (as complex as you say) and “one thing inside another”.

1 Like