Convert Scala Map to JSON with lift-json

luca-bravo-24241

Tested Env: Scala 2.11, lift-json_2.11-2.6.3

Ref: https://github.com/lift/lift/blob/master/framework/lift-base/lift-json/src/main/scala/net/liftweb/json/JsonAST.scala#L327

1. Normal Scala Map

m ‘s type: Map[String, Any]

2. Use lift-json’s implicit

Cast Map[String, Any] to Map[String, JValue] explicitly.

3. Convert Map[String, JValue] to JObject

4. Generate String from JObject

5. But!

It cannot convert List that contains Map (List[Map]), since lift-json doesn’t have implicit function for converting Map to JValue. I have tried several implicit functions, but all of them failed.

So you need to traverse through List to convert Map to JValue (pre-processing), or explicitly convert values by calling implicit function directly with match expression.

Leave a Reply