Skip to content

Instantly share code, notes, and snippets.

@satyagraha
Created October 20, 2023 14:02
Show Gist options
  • Save satyagraha/cbb818b2edd27ebe38d72bfd0f58ee11 to your computer and use it in GitHub Desktop.
Save satyagraha/cbb818b2edd27ebe38d72bfd0f58ee11 to your computer and use it in GitHub Desktop.
Circe Transform App
package example
import io.circe._
import io.circe.parser._
import io.circe.optics.JsonOptics._
import monocle.function.Plated
object CirceTransformApp {
def main(args: Array[String]): Unit = {
val json: Json = parse(
"""
{
"Order": {
"Customer": {
"Name": "Custy McCustomer",
"ContactDetails": {
"Address": "1 Fake Street, London, England",
"Phone": "0123-456-789"
}
},
"Items": [{
"Id": 123,
"Description": "banana",
"Quantity": 1
}, {
"Id": 456,
"Description": "apple",
"Quantity": 2
}],
"Total": 123.45
}
}
""").getOrElse(Json.Null)
val result = Plated.transform[Json] { j =>
// println(j)
// println()
j.asObject match {
case Some(obj) => Json.fromFields(obj.toIterable.map { case (key, value) =>
val c = key.toCharArray
c(0) = Character.toLowerCase(c(0))
new String(c) -> value })
case None => j
}
}(json)
println(result)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment