0

I tried converting Map[String, Map[String, Any]] in Scala to JSON but since they have nested maps so unable to do it. Is there a way to do it? I tried looking at this link but it converts everything to string and converts a list as "List(...)". Any ideas how to go about this? I am even fine with solutions of Map[String, Map[String, String]].

2 Answers2

1

You can use play-json library. Then converting nested maps would look like this:

import play.api.libs.json.{JsValue, Json}

val nestedMap: Map[String, Map[String, String]] = Map("employees" -> Map("Paul" -> "developer", "Alice" -> "accountant"))
val json: JsValue = Json.toJson(nestedMap)
val compactJson: String = Json.stringify(json)

println(compactJson)

Output:

{"employees":{"Paul":"developer","Alice":"accountant"}}
Paweł Jurczenko
  • 4,431
  • 2
  • 20
  • 24
  • Thanks. I also saw that one can do compact(render(decompose(nestedMap))). Here I am talking about the netliftweb.json library. – Meet Vadera Jul 19 '16 at 16:22
0

Thanks. I also saw that one can do compact(render(decompose(nestedMap))). Here I am talking about the netliftweb.json library