Parsing JSON
Turn text into JSON values.
Parsing JSON is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Circe?
Circe is a popular JSON library for Scala, built on top of the cats functional ecosystem.
It favors type-safety and immutability: parsing returns an Either rather than throwing, and decoding produces typed values instead of untyped maps.
This lesson covers turning raw JSON text into Circe's Json model.
Adding the Dependency
Circe ships as several modules. For parsing and the core model you need circe-core and circe-parser.
The circe-generic module adds automatic codec derivation, covered in later lessons.
libraryDependencies ++= Seq(
"io.circe" %% "circe-core" % "0.14.6",
"io.circe" %% "circe-parser" % "0.14.6"
)The parse Function
The entry point for reading JSON is io.circe.parser.parse.
It takes a String and returns Either[ParsingFailure, Json]. The left side captures syntax errors; the right side holds the parsed tree.
import io.circe.parser._
val result = parse("{\"name\": \"Ada\", \"age\": 36}")
// result: Either[ParsingFailure, Json]Handling Parse Failures
Because parse returns an Either, you handle errors explicitly with pattern matching or combinators instead of try/catch.
A ParsingFailure includes a human-readable message describing the syntax problem.
parse("{ not valid }") match {
case Right(json) => println(json)
case Left(err) => println(s"Failed: ${err.message}")
}The Json Model
A successfully parsed value is a Json — an immutable tree representing one of six JSON types: object, array, string, number, boolean, or null.
You inspect it with predicates like isObject or fold over its shape.
val json = parse("[1, 2, 3]").getOrElse(Json.Null)
println(json.isArray) // true
println(json.isObject) // falseNavigating with the Cursor
To read into nested data, call .hcursor on a Json value. The HCursor is a zipper that lets you move through the tree.
Use downField to descend into an object key.
val json = parse("{\"user\": {\"name\": \"Ada\"}}").toOption.get
val cursor = json.hcursor
val name = cursor.downField("user").downField("name").as[String]
// name: Either[DecodingFailure, String] = Right(Ada)Extracting Values with as
Once positioned, .as[A] attempts to decode the focused value into type A.
It returns Either[DecodingFailure, A], so a type mismatch is reported as a value, not an exception.
val json = parse("{\"age\": 36}").toOption.get
val age = json.hcursor.downField("age").as[Int]
println(age) // Right(36)parse vs decode
Use parse when you want the raw Json tree to inspect manually.
Use decode[A] when you want to go straight from String to a typed value A in one step. It combines parsing and decoding.
import io.circe.parser.decode
val n: Either[io.circe.Error, Int] =
decode[Int]("42")
println(n) // Right(42)Optional Fields
Real-world JSON often has missing keys. The cursor method get[A] on a downed field fails if absent, while getOrElse supplies a default.
Decoding into Option[A] treats a missing or null field as None.
val json = parse("{\"name\": \"Ada\"}").toOption.get
val nick = json.hcursor.get[Option[String]]("nickname")
println(nick) // Right(None)Parsing Arrays
You can decode a JSON array directly into a Scala collection by asking for List[A] or Vector[A].
Circe maps each element and short-circuits with a DecodingFailure if any element is the wrong type.
import io.circe.parser.decode
val xs = decode[List[Int]]("[1, 2, 3]")
println(xs) // Right(List(1, 2, 3))Pretty Printing
Any Json value renders back to text with .noSpaces for compact output or .spaces2 for indented output.
This round-trips parsed data and is handy for logging or debugging.
val json = parse("{\"a\":1,\"b\":2}").toOption.get
println(json.noSpaces)
println(json.spaces2)Quick Check
Test your understanding of Circe parsing.
Recap
You learned to turn JSON text into Circe's model. parse yields Either[ParsingFailure, Json]; decode[A] goes straight to a typed value.
The HCursor navigates with downField, and .as[A] extracts typed values. Failures are values, not exceptions.
Frequently asked questions
Is the “Parsing JSON” lesson free?
Yes — the full text of “Parsing JSON” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.
What will I learn in “Parsing JSON”?
Turn text into JSON values. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Scala for Backend Engineering & Functional Programming?
No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Parsing JSON” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?
Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.