How do you serialize a Map to JSON in Scala?

Jay Taylor picture Jay Taylor · Jun 7, 2011 · Viewed 43.7k times · Source

So I have a Map in Scala like this:

val m = Map[String, String](
    "a" -> "theA",
    "b" -> "theB",
    "c" -> "theC",
    "d" -> "theD",
    "e" -> "theE"
)

and I want to serialize this structure into a JSON string using lift-json.

Do any of you know how to do this?

Answer

Raja picture Raja · Jan 24, 2015

If you are using the latest Scala 2.10.x and above :

println(scala.util.parsing.json.JSONObject(m))