JSON to Scala Case Class with Circe Codec — Free Online
Convert JSON to a Scala case class with circe automatic codec derivation or play-json Format using AI. Handles nested types and Option fields. Free, no sign-up.
JSON to Scala Case Class with Circe Codec — Free Online
About JSON to Scala Case Class with Circe Codec — Free Online
JSON to Scala Case Class converts a JSON object into Scala case class definitions with circe or play-json codec derivation, mapping JSON types to their Scala equivalents. Developers use it to scaffold typed domain models from API responses in Scala backend services, Spark jobs, or Akka-based applications without writing repetitive boilerplate.
JSON to Scala Type Mapping
| JSON Type | Scala Type | Notes |
|---|---|---|
| string | String | |
| integer | Int / Long | Use Long for values > 2^31 |
| float | Double / Float | Double preferred for precision |
| boolean | Boolean | |
| null | Option[T] | Nullable fields wrapped in Option |
| array | List[T] / Seq[T] | T inferred from element type |
| object | Nested case class | New case class generated |
| ISO date string | String or java.time.Instant | Add custom codec for Instant |
Scala JSON Library Comparison
| Feature | circe | play-json | spray-json | upickle |
|---|---|---|---|---|
| Derivation style | Automatic (semiauto/auto) | Format.apply macro | jsonFormat macro | ReadWriter derive |
| Cats/FP integration | Native | No | No | No |
| Error handling | DecodingFailure | JsError | DeserializationException | AbortException |
| Akka HTTP support | akka-http-circe | play-json native | native | akka-http-upickle |
| Scala 3 support | Yes | Yes | Partial | Yes |
Frequently Asked Questions
How do I decode JSON to a Scala case class with circe?
Add io.circe:circe-generic and io.circe:circe-parser to your build.sbt. Import io.circe.generic.auto._ for automatic derivation. Then: import io.circe.parser._; parse(jsonString).flatMap(_.as[MyCaseClass]). The result is Either[Error, MyCaseClass] — use fold or getOrElse to handle errors.
How do I handle Option fields in Scala JSON deserialization?
Wrap nullable fields with Option[T]: val email: Option[String] = None. With circe, Option fields are automatically treated as optional — missing JSON keys produce None and present keys produce Some(value). This avoids NullPointerExceptions for fields that may be absent in the API response.
What is the difference between circe auto and semiauto derivation?
With auto derivation (import io.circe.generic.auto._), codecs are derived implicitly for all case classes in scope. With semiauto (import io.circe.generic.semiauto._), you explicitly call deriveDecoder[T] and deriveEncoder[T], giving more control and avoiding accidental derivation. Semiauto is recommended for production to keep compile times manageable in large codebases.
How do I serialize a Scala case class to JSON with circe?
Import io.circe.syntax._ and call caseClassInstance.asJson to get a Json value, then call .noSpaces or .spaces2 for the string output. Example: import io.circe.syntax._; val json = MyClass("Alice", 30).asJson.spaces2. The Encoder instance must be in scope via auto derivation or deriveEncoder[MyClass].
How do I map snake_case JSON keys to camelCase Scala fields?
With circe, use a custom configuration: implicit val config: Configuration = Configuration.default.withSnakeCaseMemberNames; then @ConfiguredJsonCodec case class MyClass(userId: String). With play-json, configure a custom Reads that maps each snake_case key to its camelCase equivalent. Both approaches let Scala code use idiomatic camelCase while the JSON stays snake_case.
If jsondecode.com saved you time, share it with your team
Free forever. No ads. No sign-up. Help other developers find it.