Convert JSON to Scala Case Classes — Numio
Turns a JSON sample into Scala case classes. Nested objects become separate case classes and arrays Seq[...]; null values and keys missing from some array items become Option[...].
Drafting models for a JSON API before adding a Circe, Play JSON or uPickle codec.
JSON
Scala Case Class
When to use it
- Drafting models for a JSON API before adding a Circe, Play JSON or uPickle codec.
- Turning a sample Kafka or HTTP payload into case classes for a Spark or Akka job.
How to use
- Paste a JSON object; for optional fields include several array items with different keys.
- Check the output: the root class is RootInterface and nested classes are named from their keys in PascalCase (speakers becomes Speakers).
- Copy the classes, rename them, and narrow any Option[Any] or Seq[Any] the sample could not type.
JSON input
{
"id": 7,
"type": "conference",
"price": 49.9,
"venue": null,
"tags": ["jvm", "fp"],
"speakers": [
{ "name": "Martin", "talk": "Keynote" },
{ "name": "Li", "talk": "Effects", "slides": "https://example.com/s.pdf" }
]
}Scala case classes
case class RootInterface( id: Int, `type`: String, price: Double, venue: Option[Any], tags: Seq[String], speakers: Seq[Speakers] ) case class Speakers( name: String, talk: String, slides: Option[String] )
Details and limitations
Runs in the browser with the site's own generator; no server call. The page has no settings and adds no imports or codec derivations.
All items of an array are merged: a key missing from some items, or null in some, becomes Option[...], and 12 next to 1.5 becomes Double.
A value that is only ever null gives Option[Any] and an empty array Seq[Any], because the sample has no type to show.
Reserved words and keys that are not identifiers are wrapped in backticks, such as `type`; snake_case keys are kept as written.
FAQ
Does it generate Option for nullable fields?
Yes. A key whose value is null becomes Option[...], typed from the other samples when the key also has real values, otherwise Option[Any]. Keys missing from some array items are also Option.
What happens with a JSON array at the root?
Its items are merged into one RootInterface case class, with keys missing from some items as Option, and type RootInterfaceList = Seq[RootInterface] is added.
Is a 64-bit ID typed as Long?
Only when a sample value is outside the Int range (about ±2.1 billion). Other whole numbers become Int, so change IDs or timestamps to Long if they can grow.