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
  • 1
    You can have a look at json4s.org Commented Jul 12, 2016 at 13:45
  • Why do you have Any? Commented Jul 12, 2016 at 13:48

2 Answers 2

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"}}
Sign up to request clarification or add additional context in comments.

1 Comment

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.