osleonard
osleonard

Reputation: 595

Parse Json from snake case to camel case

So I have a JSON in this format which I am trying to convert from snake case to camel case matching a corresponding case class

 val json = """
      {
        "items": [
        {
          "id": "7913",
          "route_id": "33",
          "predictable": true,
          "run_id": "33_486_1",
          "latitude": 34.0234949,
          "longitude": -118.398712,
          "heading": 236,
          "seconds_since_report": 59
        },
        {
          "id": "4140",
          "route_id": "76",
          "predictable": true,
          "run_id": "76_174_0",
          "latitude": 34.0331945,
          "longitude": -118.2646534,
          "heading": 122,
          "seconds_since_report": 12
        },
        {
          "id": "7620",
          "route_id": "20",
          "predictable": true,
          "run_id": "20_669_0",
          "latitude": 34.013733,
          "longitude": -118.490067,
          "heading": 334,
          "seconds_since_report": 172
        }
        ]
      }
      """.stripMargin

which I want converted to

  final case class Sample(
    id: Int,
    routeId: Int,
    predictable: Boolean,
    runId: String,
    latitude: Double,
    longitude: Double,
    heading: Int,
    secondsSinceReport: Int
  )

Tried using

implicit val sampleDecoder = Decoder[List[Sample]].prepare(_.downField("items"))
val decodingResult = parser.decode(json)(sampleDecoder)

but the result comes as

Attempt to decode value on failed cursor: DownField(routeId),DownArray,DownField(items)

however if I comment out the fields with camel case from the case class I get

Sample(7913,true,34.0234949,-118.398712,236)
Sample(4140,true,34.0331945,-118.2646534,122)
Sample(7620,true,34.013733,-118.490067,334)

Upvotes: 1

Views: 3489

Answers (3)

osleonard
osleonard

Reputation: 595

Finally figured it out without


implicit val encoder : Encoder[Sample] = 
    Encoder.forProduct8(
      "id",
      "route_id",
      "predictable",
      "run_id",
      "latitude",
      "longitude",
      "heading",
      "seconds_since_report"
    )(Sample.unapply(_).get)

  implicit val decoder : Decoder[Sample] =
    Decoder.forProduct8(
      "id",
      "route_id",
      "predictable",
      "run_id",
      "latitude",
      "longitude",
      "heading",
      "seconds_since_report"
    )(Sample.apply)



   implicit val sampleDecoder = Decoder[List[Sample]].prepare(_.downField("items"))
   val decodingResult = parser.decode(json)(sampleDecoder)

   decodingResult match {
        case Left(value) => println(value.getMessage())
        case Right(value) => value.foreach(println)
   }

And the result matches the supplied json

Sample(7913,33,true,33_486_1,34.0234949,-118.398712,236,59)
Sample(4140,76,true,76_174_0,34.0331945,-118.2646534,122,12)
Sample(7620,20,true,20_669_0,34.013733,-118.490067,334,172)

Upvotes: 1

Saskia
Saskia

Reputation: 1056

Your use case is straight out of the docs: https://circe.github.io/circe/codecs/custom-codecs.html

import io.circe.generic.extras._, io.circe.syntax._
// import io.circe.generic.extras._
// import io.circe.syntax._

implicit val config: Configuration = Configuration.default.withSnakeCaseMemberNames
// config: io.circe.generic.extras.Configuration = Configuration(io.circe.generic.extras.Configuration$$$Lambda$9172/0x0000000801132040@69e0f3f6,io.circe.generic.extras.Configuration$$$Lambda$9171/0x0000000801133040@66433b0e,false,None,false)

@ConfiguredJsonCodec case class User(firstName: String, lastName: String)
// defined class User
// defined object User

User("Foo", "McBar").asJson
// res1: io.circe.Json =
// {
//   "first_name" : "Foo",
//   "last_name" : "McBar"
// }

You need the generic-extras dependency in build.sbt:

libraryDependencies ++= Seq(
  "io.circe" %% "circe-core",
  "io.circe" %% "circe-generic",
  "io.circe" %% "circe-generic-extras",
  "io.circe" %% "circe-parser"
).map(_ % circeVersion)

addCompilerPlugin("org.scalamacros" %% "paradise" % "2.1.1" cross CrossVersion.full)

The docs say you don't need the compiler plugin in 2.13.x but I haven't been able to compile it. Also paradise is not published yet for 2.13.x. So this solution only works in 2.12 and earlier (yet).

If it is a one off thing, you can do the conversion manually without compiler macros:


   implicit val decodeSample: Decoder[Sample] = new Decoder[Sample] {
     final def apply(c: HCursor): Decoder.Result[Sample] =
       for {
         id <- c.downField("id").as[Int]
         routeId <- c.downField("route_id").as[Int]
         predictable <- c.downField("predictable").as[Boolean]
         runId <- c.downField("run_id").as[String]
         latitude <- c.downField("latitude").as[Double]
         longitude <- c.downField("longitude").as[Double]
         heading <- c.downField("heading").as[Int]
         secondsSinceReport <- c.downField("seconds_since_report").as[Int]
       } yield {
         new Sample(id, routeId, predictable, runId, latitude, longitude, heading, secondsSinceReport)
       }
   }

Upvotes: 2

ejisan
ejisan

Reputation: 111

If you are ok with another JSON library, "play-json" would do. It is a standalone library.

final case class Sample(
    id: String,
    routeId: String,
    predictable: Boolean,
    runId: String,
    latitude: Double,
    longitude: Double,
    heading: Int,
    secondsSinceReport: Int)

final case class Samples(items: Seq[Sample])
import play.api.libs.json._

implicit val jsonCaseConversion = JsonConfiguration(JsonNaming.SnakeCase)
implicit val sampleJsonFormat = Json.format[Sample]
implicit val samplesJsonFormat = Json.format[Samples]

val json = """
  {
    "items": [
    {
      "id": "7913",
      "route_id": "33",
      "predictable": true,
      "run_id": "33_486_1",
      "latitude": 34.0234949,
      "longitude": -118.398712,
      "heading": 236,
      "seconds_since_report": 59
    },
    {
      "id": "4140",
      "route_id": "76",
      "predictable": true,
      "run_id": "76_174_0",
      "latitude": 34.0331945,
      "longitude": -118.2646534,
      "heading": 122,
      "seconds_since_report": 12
    },
    {
      "id": "7620",
      "route_id": "20",
      "predictable": true,
      "run_id": "20_669_0",
      "latitude": 34.013733,
      "longitude": -118.490067,
      "heading": 334,
      "seconds_since_report": 172
    }
    ]
  }
""".stripMargin

val optSamples = Json.parse(json).asOpt[Samples]

I've changed Sample.id to String type. If you want this to be Int, easiest way is to add a method like this

// this might thorw an exception.
// maybe there is a reason why id in json is string
def intId: Int = id.toInt

Upvotes: 1

Related Questions