Question: 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: 
How about this?

```
implicit val formats = net.liftweb.json.DefaultFormats
import net.liftweb.json.JsonAST._
import net.liftweb.json.Extraction._
import net.liftweb.json.Printer._
val m = Map[String, String](
    "a" -> "theA",
    "b" -> "theB",
    "c" -> "theC",
    "d" -> "theD",
    "e" -> "theE"
)
println(compact(render(decompose(m))))

```

output:

```
{"e":"theE","a":"theA","b":"theB","c":"theC","d":"theD"}

```

EDIT:

For a `scala.collections.mutable.Map`, you should convert it first to an immutable map: `.toMap`
This code will convert many different objects, and doesn't require any libraries beyond the built-in the `scala.util.parsing.json._`. It won't properly handle edge cases like Maps with integers as keys.

```
import scala.util.parsing.json.{JSONArray, JSONObject}
def toJson(arr: List[Any]): JSONArray = {
  JSONArray(arr.map {
    case (innerMap: Map[String, Any]) => toJson(innerMap)
    case (innerArray: List[Any])      => toJson(innerArray)
    case (other)                      => other
  })
}
def toJson(map: Map[String, Any]): JSONObject = {
  JSONObject(map.map {
    case (key, innerMap: Map[String, Any]) =>
      (key, toJson(innerMap))
    case (key, innerArray: List[Any]) =>
      (key, toJson(innerArray))
    case (key, other) =>
      (key, other)
  })
}

```