🙋Introduction
This article will take us through JSON basics, the Play JSON library, JSON Reads/Writes/Format Combinators, JSON automated mapping, and JSON transformers in Scala. Java developers and Scala developers use Play for developing web applications. It unifies the API and components needed for developing a web application.

Let us start with understanding JSON basics in Scala.
JSON Basics🤓
JSON (JavaScript Object Notation) data is often parsed and generated in modern online applications. Play has a JSON library that supports it. JSON is a simple data-interchange format. Look at this first code snippet to see how JSON data looks like:
{
"firm" : "Coding Ninjas",
"location" : {
"lat" : 28.4595,
"long" : 77.0266
},
"employees" : [ {
"name" : "ABC",
"age" : 18,
"position" : null
}, {
"name" : "XYZ",
"age" : 22,
"position" : "Intern"
} ]
}
The Play JSON Library
To use JSON data, Play has a package called play.api.libs.json. The play.api.libs.json package provides JSON data representation data structures and utilities for converting between these data structures and other data representations.

This package includes the following features:
- Automatic conversion to and from case classes is possible.
- It supports custom validation while parsing.
- It is highly customizable.
The package provides three types of data :
- JsValue: This can represent any JSON value. We may create a representation of any JSON structure using the different JsValue types.
- Json: It is an object used for converting from JsValue structures.
-
JsPath: This helps traverse the patterns for implicit conversions and JsValue structures.
Now let us see all the ways we can convert to a JsValue:
Conversion to JsValue
1. Using String Parsing:
import play.api.libs.json._
val json: JsValue = Json.parse("""
{
"firm" : "Coding Ninjas",
"location" : {
"lat" : 28.4595,
"long" : 77.0266
},
"employees" : [ {
"name" : "ABC",
"age" : 18,
"position" : null
}, {
"name" : "XYZ",
"age" : 22,
"position" : "Intern"
} ]
}
""")
2. Using Class Construction
import play.api.libs.json.JsNull
import play.api.libs.json.Json
import play.api.libs.json.JsString
import play.api.libs.json.JsValue
val json: JsValue = Json.obj(
"firm" -> "Coding Ninjas",
"location" -> Json.obj("lat" -> 28.4595, "long" -> 77.0266),
"employees" -> Json.arr(
Json.obj(
"name" -> "ABC",
"age" -> 18,
"position" -> JsNull
),
Json.obj(
"name" -> "XYZ",
"age" -> 22,
"position" -> "Intern"
)
)
)
3. Using Writes Converters
The utility method Json.toJson(T) helps convert Scala to JsValue. Implicit Writes is available in the Play JSON API for basic types like int, double, boolean, and string. E.g.:
import play.api.libs.json._
val Name = Json.toJson("ABC")
val Age = Json.toJson(18)
val Bool= Json.toJson(false)
// collections of basic types
val i_array = Json.toJson(Seq(18,22))
val s_array = Json.toJson(List("ABC", "XYZ"))
For converting our models to JsValues, we must define implicit Writes converters and then provide them in scope. For eg, for location model:
case class Location(lat: Double, long: Double)
import play.api.libs.json._
implicit val locationWrites = new Writes[Location] {
def writes(location: Location) = Json.obj(
"lat" -> location.lat,
"long" -> location.long
)
}
Now let us discuss how we can traverse a JsValue structure and then proceed to see how we can convert from JsValue.
Traversing a JsValue structure
Using the \ operator, we can obtain the value of the argument specified in the JsObject.
val emp_one = (json \ "employees" \ 1).get
// returns {"name":"XYZ","age":22,"role":"Intern"}
Using the \\ operator, we can obtain the value of the specified JsObject and all its descendants.
Now let us proceed to see how we can convert from JsValue.
Conversion From JsValue
1. Using string utilities
a. Minified
val minifiedString: String = Json.stringify(json)
b. Readable
val readableString: String = Json.prettyPrint(json)
2. Using JsValue.as/asOpt
- using JsValue.as[T]
val firm = (json \ "firm").as[String]
// "Coding Ninjas"
val names = (json \\ "name").map(_.as[String])
// Seq("ABC", "XYZ")
This method JsValue.as[T] throws an exception if a conversion is impossible or the specified path is unavailable. To overcome this we use another method JsValue.asOpt[T].
-using JsValue.asOpt[T]
val firm = (json \ "firm").asOpt[String]
// Some("Coding Ninjas")
3. JsValue to a model
To convert from JsValue to the model, we need to implicitly define Reads[T].
case class Location(lat: Double, long: Double)
import play.api.libs.json._
import play.api.libs.functional.syntax._
implicit val lR: Reads[Location] = (
(JsPath \ "lat").read[Double] and
(JsPath \ "long").read[Double]
)(Location.apply _)
Play supports HTTP requests and responses with JSON content by combining the HTTP API with the JSON library. We'll walk through the principles by creating a small RESTful web service. For all data, the service will use the JSON content type.