JSON to Scala Converter
Generate Scala case classes from a JSON sample, with Option for nullable fields
JSON Input
Scala Output
What does the JSON to Scala converter generate?
Before a JSON library can decode anything in Scala, you need the case classes it decodes into. Paste a JSON sample and this page writes them: your package line, then one case class per object with camelCase fields, nested classes first. Whole numbers are Int, Long once a value passes 32 bits, and BigInt beyond the 64-bit range; arrays become List[T], nested arrays List[List[T]].
With Option Types on, any field that was null, or missing from some objects in an array, is typed Option[T]. Keys that are Scala 2 or Scala 3 reserved words, like type, class or given, keep their name in backticks (`type`: String), so they still match the JSON key. Keys that have to be renamed, like user-id to userId, get a // JSON key: "user-id" comment so you know where to add a rename in your codec.
The output is just the case classes. There are no Circe, Play JSON or uPickle codecs or implicits; add them the way your library expects, for example deriveDecoder in Circe or Json.format in Play JSON. Values whose type varied, or that were only null, are typed Any, which has no codec, so replace those with a real type or your library's JSON value type. Untick Case Class for plain classes with val parameters. It all runs in your browser.
How to use it
The classes update as you type.
Paste a JSON sample
Paste into the JSON Input panel, Upload a .json or .txt file, or click Sample. Example input:
{"subscriberId": "SUB-1001", "plan": "Unlimited 5G", "dataUsage": 45.2, "status": "active"}Set Class Name for the root class and Package for the package line. If the input won't parse, check it with the JSON Validator.
Read the case classes
The Scala Output panel shows the nested classes first. In the sample, the device class has a field named `type` in backticks because type is reserved.
Copy or download
Copy the code or Download a .scala file, then derive the codecs for your JSON library.
Where this saves time
HTTP clients with sttp or http4s
Paste a response from the API you call, generate the case classes, derive Circe decoders and decode the response body straight into Order.
Play Framework controllers
Generate the request body type for a JSON endpoint, add an implicit Json.format[CreateOrder], and validate with request.body.validate[CreateOrder].
Spark schemas
Case classes from a sample JSON record can back a typed Dataset, for example spark.read.json(path).as[Event], once the field types match your data.
Finding optional fields
Paste a few real records as an array. Fields that are absent or null in any of them come out as Option, which tells you what to handle.
Common Questions
Does it generate Circe or Play JSON codecs?
No. It generates the case classes only. Add deriveDecoder/deriveEncoder for Circe, Json.format for Play JSON, or derives ReadWriter for uPickle in Scala 3.
Does it work with Scala 3?
Yes. Backticks cover reserved words from both Scala 2 and Scala 3 (given, then, enum and so on), and the syntax is valid in both. There is no Scala compiler behind this page, so compile it once in your project.
What does Option Types change?
On: fields that were null or missing in the sample become Option[T]. Off: they keep the plain type. A field that was only ever null becomes Option[Any] or Any, which you should replace with a real type.
How are big numbers typed?
The page reads integers exactly. An ICCID like 8901240544102066246 becomes Long, and anything beyond the 64-bit range becomes BigInt.
What if my root is an array?
The objects inside it become the root class, and a comment shows the List[RootObject] to decode into. A root that is null, a string or a list of plain values gives a short message instead of code.
Is my JSON uploaded?
No. The classes are generated by JavaScript in your browser and nothing is sent to a server. Input is capped at 256 KB.
Other Code Generation Tools
Need the same payload in another JVM language, or want to validate it first?