General purpose JSON library for parsing and building JSON.
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


JSONAR is a JSON library for Scala, offering the ability to parse text for JSON into an object graph and similarly turn an object graph into a String. It avoids pitfalls of some JSON libraries, like the inbuilt Scala implementation, by offering the following features:

  • Parsing preserves the errors without throwing exceptions, making for simpler code as you just need to map or flatMap the result.
  • Rigidly typing the JSON objects avoiding types like Map[String, Any], to ensure a List couldn't be used as the key in a JSONObject.
  • Sensible printing of JSON from the graph, as stock Scala provides none, with tests to ensure that going forwards and backwards parsing and printing produces the same results.
  • Simple easy to follow code.


To include it in your SBT project add the following to the definition

resolvers += "Sonatype Repo" at ""

libraryDependencies += "com.github.seanparsons.jsonar" %% "jsonar" % "0.9.1"

The first thing you'll want to do is import the core of jsonar, which will include everything you need including lots of convenience implicits:

import com.github.seanparsons.jsonar._

To parse some JSON use the Parser object:

val parseResult: ValidationNEL[String, JSONValue] = Parser.parse("[10]")
// scala> parseResult: ValidationNEL[String, JSONValue] = Success(JSONArray(Vector(JSONInt(10))))

To produce JSON from an instance of JSONValue use the Printer object:

val json: String = Printer.print(JSONObject(JSONString("key") -> JSONString("value")))
// scala> json: String = {"key":"value"}

A XPath-like API is available which lends itself to for comprehensions very nicely:

val json = """

val friendsOfUser = for {
  parsed <- Parser.parse(json)
  user <- (parsed \ "users" \ "1").asJSONString
  friendIDArray <- (parsed \ "friends" \ "1").asJSONArray
  friendIDs <- friendIDArray.collectElements{case JSONInt(value) => value}
} yield (user.value, friendIDs)


JSONAR uses Scalaz for the ValidationNel type which is effectively a pimped version of the Either class.

The core is all inside the Types.scala file which is well worth skimming over quickly.