JSON to Scala

Input
Output

JSON to Scala

Paste a JSON sample, such as an API response, and Bracely writes Scala case classes with circe codecs for it. Each object becomes a final case class with a companion object holding circe Decoder and Encoder instances, for http4s, Akka HTTP or any circe-based code.

Example: a UPI payment request

One request object, with type (a Scala keyword, so it's written in backticks) and a note that's only ever null, which becomes circe's Json until you know its type:

Input

{
  "requestId": "req_9001",
  "amountInPaise": 149900,
  "currency": "INR",
  "payer": {
    "name": "Ananya Rao",
    "vpa": "ananya@upi"
  },
  "type": "collect",
  "expiresInSeconds": 600,
  "note": null
}

Output

import io.circe.{Decoder, Encoder, Json}
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}

final case class Payer(
  name: String,
  vpa: String
)

object Payer {
  implicit val decoder: Decoder[Payer] = deriveDecoder
  implicit val encoder: Encoder[Payer] = deriveEncoder
}

final case class PaymentRequest(
  requestId: String,
  amountInPaise: Long,
  currency: String,
  payer: Payer,
  `type`: String,
  expiresInSeconds: Long,
  note: Json
)

object PaymentRequest {
  implicit val decoder: Decoder[PaymentRequest] = deriveDecoder
  implicit val encoder: Encoder[PaymentRequest] = deriveEncoder
}

How do I use the JSON to Scala?

  1. Paste a JSON sample.
  2. Set the root type name, for example Order.
  3. Copy the Scala code into your project.

What does the JSON to Scala do?

  • case classes with Long, Double, String and Boolean
  • Option[...] = None for fields that can be missing
  • circe deriveDecoder and deriveEncoder for each class
  • Nested objects become their own named types
  • 20 other languages from the same sample, including TypeScript

When to use the JSON to Scala

Scala services that use circe for JSON, with http4s, Akka HTTP, Pekko HTTP or on their own, need a case class and a codec for every payload. Paste a real request or response and copy case classes with their circe Decoder and Encoder already written.

Each object becomes a final case class with a companion object holding deriveDecoder and deriveEncoder. Whole numbers become Long and decimals Double; a field missing from some items becomes Option with a None default. Names that are Scala keywords, like type in the example, are written in backticks so they still match the JSON. A field that's only ever null in the sample becomes circe's Json until you know what it holds.

Frequently asked questions

How do I decode JSON into it?
With circe-parser: io.circe.parser.decode[PaymentRequest](text) returns Either an error or the value. To encode, call request.asJson from io.circe.syntax._.
Does it work with Scala 3?
Yes. The codecs use circe's semiauto derivation, which works in Scala 2.13 and Scala 3. A top-level JSON array is noted in a comment (decode it as List[...]) rather than a type alias, which only compiles in Scala 3.
How does it pick Scala types?
From the values in your sample: whole numbers, decimals, text, true/false, arrays and nested objects. When array items disagree, the field is made optional or widened.
Can I generate other languages?
Yes. Change Language to any of the others: TypeScript, Go, Python, Pydantic, Java, C#, Kotlin, Swift, Rust, Ruby, Dart, PHP, Zod, TypeBox, Valibot, Yup, GraphQL, Protobuf, Prisma and Drizzle.
Is my data uploaded?
No. Bracely runs entirely in your browser, so nothing you paste or open leaves your device.

More JSON tools

Bracely has 104 JSON tools in one page. Related ones:

Runs entirely in your browser. Nothing you paste is uploaded. Made by Saurabh Pagrut · About · Privacy policy
Last updated · Version 6.1 · Changelog
Saurabh Pagrut on GitHub · LinkedIn · X · YouTube · Instagram