About the JSON to Scala
Generate Scala case classes from JSON, with circe decoders and encoders or Play JSON formats in the companion objects. Field names match your JSON keys exactly.
Key features
- Scala case classes from any JSON sample
- circe semi-auto decoders and encoders, or Play JSON formats
- Option[T] fields with None defaults
- Int vs Long chosen from your values
- Field names match JSON keys exactly (backticks where needed)
- Companion objects with implicit codecs
Scala case classes from JSON
Scala services built on http4s, Akka HTTP, Pekko or Play typically decode JSON into case classes. Paste a sample and get case classes with codecs for circe or Play JSON.
Example
[
{ "user_id": 7, "name": "Ada", "nickname": "ada", "score": 4.5 },
{ "user_id": 8, "name": "Alan", "nickname": null, "score": 3 }
]
import io.circe.{Decoder, Encoder, Json}
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
final case class User(
user_id: Int,
name: String,
nickname: Option[String] = None,
score: Double
)
object User {
implicit val decoder: Decoder[User] = deriveDecoder
implicit val encoder: Encoder[User] = deriveEncoder
}
val users = io.circe.parser.decode[List[User]](json)
Libraries
| Option | Companion object provides | Decode with |
|---|---|---|
| circe | Decoder + Encoder | decode[User](json) |
| Play JSON | OFormat with default values | Json.parse(json).as[User] |
| None | — | Bring your own codecs |
Tips
- camelCase fields: with circe-generic-extras, configure
Configuration.default.withSnakeCaseMemberNamesand rename fields freely. - Dates: change
Stringtojava.time.Instant; circe and Play both include codecs for it. - Big numbers: use
BigDecimalfor money.
Related tools
Generating for the JVM in other languages? See JSON to Java and JSON to Kotlin.
How to use the JSON to Scala
- Paste your data into the input box, upload a file, or click Sample to try it out.
- Adjust the options above the editor if you need to (indentation, delimiter, and so on).
- The Scala appears instantly in the output panel. Any errors are shown with their line and column.
- Click Copy to copy the result, or Download to save it as a file.