Own that JSON using the powerful json4s library.
Json examples from json-schema.org.
–
Library dependencies:
(Gradle format)
imports:
–
When the top level is a JSON Object:
1. Parse directly into Scala
This is possible with the org.json4s.extract
method.
Note that method’s signature:
which requires an implicit
param of type Formats
is required. So be sure to declare an implicit param of that type in scope of the extract
call.
Extracting to Map[String, Any]
seems to work for most cases:
Leverage all the power of Scala case classes for working with data.
It is not required that the case class implement all fields of the JSON.
This is my preferred method.
–
When the top level is a JSON Array of Objects:
1. Go for the gold
more examples in this style
Again this is the preferred method. Leverage one of Scala’s best features, the case class, and own that JSON!