Loading JSON to Scala Converter...
Please wait a moment

JSON to Scala: Case Classes You Can Decode with circe

Paste a real API response and get case classes whose fields line up with its keys, Option where a value can be missing, and Long for IDs too big for an Int.

Step 1

Paste or upload your JSON

Start with JSON your service really receives. The classes can only contain what your sample shows, so a response with every optional field filled in gives the most useful types.

Example input

{
  "userId": 12345,
  "username": "scaladev",
  "email": "dev@scala-lang.org",
  "isActive": true,
  "preferences": ["functional_programming", "immutability", "type_safety"],
  "last_login": null,
  "profile": {
    "firstName": "Scala",
    "lastName": "Developer",
    "age": 32,
    "country": "Switzerland"
  }
}
Step 2

Name the root class

There are two settings above the editors. Everything else comes from your data.

•
Class Name: the name of the top-level class and of the downloaded .scala file
•
Use Case Classes: on gives case classes with a derived circe codec; off gives plain classes with val fields and a hand-written codec
•
Types: Int, then Long, then BigInt for whole numbers; Double for anything with a decimal point; quoted values like "00501" stay String
Step 3

Read the generated classes

This is the whole file for the example above. last_login was null, so it became an Option; snake_case keys like it keep their spelling, because the field name is the key.

Output for the example above

// Scala 2.13 classes with circe codecs. In build.sbt:
//   libraryDependencies ++= Seq("circe-core", "circe-generic", "circe-parser").map("io.circe" %% _ % "0.14.10")
// Read your JSON with:
//   val data = io.circe.parser.decode[Root](json)  // Either[io.circe.Error, Root]

import io.circe.{Codec, Json}
import io.circe.generic.semiauto.deriveCodec

final case class Root(
  userId: Int,
  username: String,
  email: String,
  isActive: Boolean,
  preferences: List[String],
  last_login: Option[Json] = None,
  profile: Profile
)

object Root {
  implicit val codec: Codec[Root] = deriveCodec[Root]
}

final case class Profile(
  firstName: String,
  lastName: String,
  age: Int,
  country: String
)

object Profile {
  implicit val codec: Codec[Profile] = deriveCodec[Profile]
}
Step 4

Copy or download the .scala file

Add a package line if your project uses packages, drop the file into your sbt project and decode with the line from the top comment.

•
Copy puts the whole file on your clipboard
•
Download saves it as a .scala file named after your top-level class
•
Top-level lists: the top comment switches to decode[List[Root]]
•
Check the data first: the JSON Validator points at the exact line of a syntax error

What is JSON to Scala Conversion?

In Scala you usually decode JSON straight into case classes and let a library such as circe do the mapping. Writing the classes by hand gets tedious fast, and the easy mistakes are the costly ones: an ID declared as Int that overflows, a field that turns out to be missing in some records, a key that isn't a legal Scala name.

This page writes them from a real sample. If your data is XML rather than JSON, the XML to Scala page does the same job; to tidy the JSON first, try the JSON Formatter.

Frequently Asked Questions

Which library does the generated code need?

circe 0.14 on Scala 2.13: circe-core and circe-parser to decode, plus circe-generic for deriveCodec. The comment at the top of the file has the build.sbt line and the exact io.circe.parser.decode call. We compile every case we test with scalac and decode the JSON it came from, so the classes match the data. Play JSON and other libraries are not targeted; to use Play JSON you would replace each companion object with a Json.format line yourself.

Why do some field names have backticks around them?

The field names are your JSON keys, unchanged, so circe needs no renaming at all. Scala only allows keys like first-name, user id, type or 2nd as names when they sit in backticks, so those get them. A few keys can never be a field name, such as an empty key, one with a backslash, a backtick or a line break, or a name every case class already has like toString or copy. Those are renamed (toStringValue, field, and so on), the original key is written in a comment next to them, and that class gets a short hand-written codec that maps the real key back.

How does it choose between Int, Long, BigInt and Double?

It looks at how each number is written in your JSON, across every item of a list. Whole numbers that fit in 32 bits become Int, bigger ones become Long (an ID like 8901240544102066246 keeps every digit), and anything past 64 bits becomes BigInt. A number written with a decimal point or an exponent, such as 14.50 or 1e3, becomes Double, or BigDecimal when a Double would change the value, for example 1e400 or a field that mixes decimals with very long whole numbers. Text in quotes, like "00501", stays a String so the leading zeros survive.

Why are some fields Option[...] = None?

A field becomes an Option when its value is null somewhere in your JSON or when it is missing from some items of a list. circe then decodes a missing key or a null as None, and writes None back as null. A value that is always null in your sample becomes Option[Json], and a field holding different kinds of values becomes io.circe.Json, because there is nothing more specific to go on. Paste a sample that shows every field you expect.

What if my JSON is a list rather than an object?

The root class then describes one item, built from every item in the list, and the comment at the top tells you to decode a List[Root] instead of a Root. A list of plain values such as [1, 2, 3], or a single number or string, has no object to turn into a class, so you get a short note explaining that instead of code.

Is my JSON sent anywhere?

No. The classes are generated in your browser as you type, and nothing you paste or upload is sent to our server or kept after you leave the page.