JSON to Scala Converter

JSON Input

Scala Output

Paste your JSON on the left and the Scala code shows up here.

Or drop a file on this box, , or .

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. This page writes them from a sample payload: 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]].

Turn Option Types on and any field that came through as null, or that some array elements skipped, 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. A field that held a string in one object and a number in another, or held nothing but null, is typed Any; no codec can derive for that, so swap in a concrete type or your library's own JSON value type (Json in Circe, JsValue in Play). Untick Case Class for plain classes with val parameters. The Scala source is generated in the page; no compiler runs and nothing is uploaded.

How to use it

The classes update as you type.

1

Paste a JSON sample

Paste into JSON Input, load a .json or .txt via Upload, or click Sample. Try a payment webhook event; its type key happens to be a Scala keyword:

{"eventId": "evt_88213", "type": "payment.settled", "amountCents": 259900, "merchant": {"id": "m_1042", "country": "IE"}}

Class Name names the root case class and Package fills the package line. With PaymentEvent and com.shop.events, the event above yields a Merchant class, amountCents: Int and a field written as `type`: String. Got an Invalid JSON message? The JSON Validator shows the bad line.

2

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.

3

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: a field that was null somewhere, or absent from one of the array's objects, becomes Option[T]. Off: it keeps its plain type. A field that never held anything but null has no type to infer, so it comes out as Option[Any] (or plain Any with the switch off); put the real type in before your codec derives anything.

How are big numbers typed?

Integers come straight from the source text, so a long ID isn't rounded before it is typed. Values that fit in 32 bits are Int; an account number of 4294967296 or a 19-digit ICCID like 8901240544102066246 is Long; past the 64-bit range the field becomes BigInt.

What if my root is an array?

The array's objects become the root case class, and the first line of output names the type to decode into, for example // The JSON root is an array: decode it as List[PaymentEvent]. A bare 42 or "pending" at the root has no fields, so you get a Nothing to generate note instead of classes.

Is my JSON uploaded?

No. The case classes are generated in this browser tab and the event data stays local. Input is limited to 256 KB; a couple of representative events give you the same classes a full export would.

Other Code Generation Tools

Need the same payload in another JVM language, or want to validate it first?