Reading JSON

To read the contents of JSON, we use the following code:

    import play.api.libs.json._    val jsonString = """{          | "teamName" : "Real Madrid FC",          | "players" : [ {          | "name" : "Ronaldo",          | "age" : 36          | }, {          | "name" : "Modric",          | "age" : 30          | }, {          | "name" : "Bale",          | "age" : 27          | } ],          | "location" : {          | "lat" : 40.4168,          | "long" : 3.7038          | }          |}          |""". stripMargin    val jValue: JsValue = Json.parse(jsonString)

The simplest way to parse JSON is via Json.parse. It returns  JsValue. JsValue is a sealed trait that signifies a JSON value. Its subclasses are as follows:

  • JsNull: This represents a null value
  • JsBoolean: This represents true or false
  • JsNumber: This represents an int or floating point arithmetic
  • JsString: This represents a JSON ...

Get Scala Microservices now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.