JSON to Scala 3
Paste a JSON sample. Get Scala 3 types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.
Open the tool with Scala 3 chosen
An example
This JSON:
{
"id": 42,
"name": "Ada Lovelace",
"active": true,
"joined": "2020-01-02T03:04:05Z",
"tags": ["engineer", "writer"],
"address": { "city": "London", "postcode": null },
"scores": [9.5, 8, 10]
}
becomes this Scala 3:
package quicktype
case class Root (
val id : Long,
val name : String,
val active : Boolean,
val joined : String,
val tags : Seq[String],
val address : Address,
val scores : Seq[Double]
)
case class Address (
val city : String,
val postcode : Option[Any] = None
)
With the reader and the writer
The tool also writes the code that turns the text into those types, and back again. That code checks the shape as it reads, which is the part a type alone cannot do.
package quicktype
import io.circe.syntax._
import io.circe._
import cats.syntax.functor._
// If a union has a null in, then we'll need this too...
type NullValue = None.type
case class Root (
val id : Long,
val name : String,
val active : Boolean,
val joined : String,
val tags : Seq[String],
val address : Address,
val scores : Seq[Double]
) derives Encoder.AsObject, Decoder
case class Address (
val city : String,
val postcode : Option[Json] = None
) derives Encoder.AsObject, Decoder
What you need in your project
A JSON library of your choice.
The output uses the Scala 3 syntax: a case class for each object, with no braces where the language allows that.
The trap to know about
Scala 2 will not compile the Scala 3 syntax. Check which version your project uses.
The options you can change
These belong to Scala 3, so the row of switches changes with the language.
| Option | What it does | Default |
|---|---|---|
| Types only | Plain types only | false |
| Framework | Serialization framework | circe | upickle |
| Package name | Package | quicktype |
Questions
How do I turn JSON into Scala 3?
Paste your JSON into the box, choose Scala 3, and the code appears at once. Give the top type a name, and every other name follows from your keys. Then copy the code, or download it as a file.
Does my JSON go to a server?
No. The generator runs inside your browser, in a worker. Nothing is uploaded, nothing is stored, and the tool works with the network turned off.
What do I need to add to my Scala 3 project?
A JSON library of your choice.
How does it know which fields are optional?
From your sample. A key that is missing, or holds null, becomes optional. One sample cannot show everything, so give more than one, or start from a JSON Schema.
Other languages
Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.
Open JSON to Code