1

I've a scala Map which is of form <String, Any>. My objective is to iterate over all the key of the map and create a new json object which consists of all these k,v pairs. I'm using play framework for the json library.

I tried this:

var out  = JsNull
for (str <- inp) {
    out = out.as[JsObject] + (str -> Json.toJson(inp(str)))
}

However Json.toJson returns a JsonNode whereas it is expecting a JsValue. I've looked at this SO Question for adding new items.

Question 1: How do I convert JsonNode to JsValue

Question 2: This is a bad implementation in scala. I'm using var. How do I convert the same logic so that I use a val. I'm particularly new to functional programming, so have a tough time thinking away from the usual iterative loops.

Community
  • 1
  • 1
sushant-hiray
  • 1,838
  • 2
  • 21
  • 28
  • 3
    why don't you convert the Map to json with Json.toJson instead of iterating on it ? https://www.playframework.com/documentation/2.0/ScalaJson – Maxim Jun 18 '15 at 07:36
  • It amazes me how much people don't understand how serialization of JSON works with GSON, Play etc etc. I see a question like this almost every time I get on SO. Le sigh.... – JakeWilson801 Jun 18 '15 at 21:38

0 Answers0