What JSON library to use in Scala?
Lift-json is at version 2.6 and it works really well (and is also very well supported, the maintainer is always ready to fix any bugs users may find. You can find examples using it on the github repository
The maintainer (Joni Freeman) is always reachable on the Lift mailing list. There are also other users on the mailing list who are very helpful as well.
As @Alexey points out, if you want to use the library with other Scala version, say 2.11.x
, change scalaVersion
and use %%
as follows:
scalaVersion := "2.11.5"
"net.liftweb" %% "lift-json" % "2.6"
You can check the liftweb.net site to find out the latest version as time goes by.
I suggest using jerkson, it supports most basic type conversions:
scala> import com.codahale.jerkson.Json._
scala> val l = List(
Map( "id" -> 1, "name" -> "John" ),
Map( "id" -> 2, "name" -> "Dani")
)
scala> generate( l )
res1: String = [{"id":1,"name":"John"},{"id":2,"name":"Dani"}]
Unfortunately writing a JSON library is the Scala community's version of coding a todo list app.
There are quite a variety of alternatives. I list them in no particular order, with notes:
- parsing.json.JSON - Warning this library is available only up to Scala version 2.9.x (removed in newer versions)
- spray-json - Extracted from the Spray project
- Jerkson ± - Warning a nice library (built on top of Java Jackson) but now abandonware. If you are going to use this, probably follow the Scalding project's example and use the backchat.io fork
- sjson - By Debasish Ghosh
- lift-json - Can be used separately from the Lift project
- json4s 𣠧 ± - An extraction from lift-json, which is attempting to create a standard JSON AST which other JSON libraries can use. Includes a Jackson-backed implementation
- Argonaut 𣠧 - A FP-oriented JSON library for Scala, from the people behind Scalaz
- play-json ± - Now available standalone, see this answer for details
- dijon - A handy, safe and efficient JSON library, uses jsoniter-scala under hood.
- sonofjson - JSON library aiming for a super-simple API
- Jawn - JSON library by Erik Osheim aiming for Jackson-or-faster speed
- Rapture JSON ± - a JSON front-end which can use 2, 4, 5, 6, 7, 11 or Jackson as back-ends
- circe ð£ - fork of Argonaut built on top of cats instead of scalaz
- jsoniter-scala - Scala macros for compile-time generation of ultra-fast JSON codecs
- jackson-module-scala - Add-on module for Jackson to support Scala-specific datatypes
- borer - Efficient CBOR and JSON (de)serialization in Scala
ð£ = has not fixed security vulnerabilities, § = has Scalaz integration, ± = supports interop with Jackson JsonNode
In Snowplow we use json4s with the Jackson back-end; we've had good experiences with Argonaut too.