title: Scala to JSON in Play Framework 2.1
body: <p>I'm trying to convert Scala to JSON in the 2.1RC Play Framework.</p>
  
  <p>I can do the following and get JSON:</p>
  
  <pre><code>import play.api.libs.json._
  
  val a1=Map("val1"-&gt;"a", "val2"-&gt;"b")
  Json.toJSon(a1)
  </code></pre>
  
  <p>Because a1 is just Map[String,String] that works OK.</p>
  
  <p>But if I have something more complex like where I have Map[String,Object], that doesn't work:</p>
  
  <pre><code>val a = Map("val1" -&gt; "xxx", "val2"-&gt; List("a", "b", "c"))
  Json.toJSon(a1)
  &gt;&gt;&gt; error: No Json deserializer found for type scala.collection.immutable.Map[String,Object]
  </code></pre>
  
  <p>I found that I can do something like the following:</p>
  
  <pre><code>val a2 = Map("val1" -&gt; Json.toJson("a"), "val2" -&gt; Json.toJson(List("a", "b", "c")))
  Json.toJson(a2)
  </code></pre>
  
  <p>And that works.</p>
  
  <p>But how can I do that in a general way?  I thought that I could do something like the following:</p>
  
  <pre><code>a.map{ case(k,v)=&gt; (k, Json.toJson(v) )}
  &gt;&gt;&gt; error: No Json deserializer found for type Object
  </code></pre>
  
  <p>But I still get an error that it can't be deserialized</p>
  
  <hr>
  
  <p><strong>Additional Information:</strong></p>
  
  <p>Json.toJson can convert a Map[String, String] to a JsValue:</p>
  
  <pre><code>scala&gt; val b = Map( "1" -&gt; "A", "2" -&gt; "B", "3" -&gt; "C", "4" -&gt; "D" )
  b: scala.collection.immutable.Map[String,String] = Map(1 -&gt; A, 2 -&gt; B, 3 -&gt; C, 4 -&gt; D)
  
  scala&gt; Json.toJson(b)
  res31: play.api.libs.json.JsValue = {"1":"A","2":"B","3":"C","4":"D"}
  </code></pre>
  
  <p>But, it fails in trying to convert a Map[String, Object]:</p>
  
  <pre><code>scala&gt; a
  res34: scala.collection.immutable.Map[String,Object] = Map(val1 -&gt; xxx, val2 -&gt; List(a, b, c))
  
  scala&gt; Json.toJson(a)
  &lt;console&gt;:12: error: No Json deserializer found for type scala.collection.immutable.Map[String,Object]. Try to implement an implicit Writes or Format for this type.
            Json.toJson(a)
  </code></pre>
  
  <p>Using the 'hint' from this Play Framework page on converting Scala to Json, I found the following (<a href="http://www.playframework.org/documentation/2.0.1/ScalaJson">http://www.playframework.org/documentation/2.0.1/ScalaJson</a>):</p>
  
  <p>If instead of Map[String, Object], there is a Map[String, JsValue], then Json.toJson() will work:</p>
  
  <pre><code>scala&gt; val c = Map("aa" -&gt; Json.toJson("xxxx"), "bb" -&gt; Json.toJson( List("11", "22", "33") ) )
  c: scala.collection.immutable.Map[String,play.api.libs.json.JsValue] = Map(aa -&gt; "xxxx", bb -&gt; ["11","22","33"])
  
  scala&gt; Json.toJson(c)
  res36: play.api.libs.json.JsValue = {"aa":"xxxx","bb":["11","22","33"]}
  </code></pre>
  
  <p>So, what I would like, is that given a Map[String, Object], where I know that the Object values were all originally of type String or List[String], how to apply the function Json.toJson() to the all the values in the map and get a Map[String, JsValue].</p>
  
  <p>I also found that I can filter out those values that are purely string and those that are (were) of type List[String]:</p>
  
  <pre><code>scala&gt; val a1 = a.filter({case(k,v) =&gt; v.isInstanceOf[String]})
  a1: scala.collection.immutable.Map[String,Object] = Map(val1 -&gt; xxx)
  
  scala&gt; val a2 = a.filter({case(k,v) =&gt; v.isInstanceOf[List[String]]})
  &lt;console&gt;:11: warning: non-variable type argument String in type List[String] is unchecked since it is eliminated by erasure
     val a2 = a.filter({case(k,v) =&gt; v.isInstanceOf[List[String]]})
                                                   ^
  a2: scala.collection.immutable.Map[String,Object] = Map(val2 -&gt; List(a, b, c))
  </code></pre>
  
  <p>The List[String] filtering gives a warning, but seems to give the answer I want.  If the two filters could be applied and then Json.toJson() used on the values of the result, and the results combined, maybe that would work?</p>
  
  <p>But the filtered results are still of type Map[String, Object] which causes a problem:</p>
  
  <pre><code>scala&gt; Json.toJson(a1)
  &lt;console&gt;:13: error: No Json deserializer found for type scala.collection.immutable.Map[String,Object]. Try to implement an implicit Writes or Format for this type.
            Json.toJson(a1)
  </code></pre>