title: How do you create Json object with values of different types?
body: <p>How do you create Json object with values of different types ?</p>
  
  <p>I'm using spray-json</p>
  
  <p>Here is the code</p>
  
  <pre><code>val images : List[JsObject] = fetchImageUrls(url).map((url: String) =&gt; {
    JsObject(List(
          "link_path" -&gt; JsString(url),
          "display_name" -&gt; JsString("image"),
          "size" -&gt; JsString(""),
          "modified" -&gt; JsString(""),
          "thumbnail" -&gt; JsString(url),
          "filename" -&gt; JsString("image"),
          "is_dir" -&gt; JsBoolean(x = false),
          "thumb_exists" -&gt; JsBoolean(x = true)) )
    })
  
  val jsonAst: JsObject = JsObject(List(
    "client" -&gt; JsString("urlimages"),
    "view" -&gt; JsString("thumbnails"),
    "contents" -&gt; JsArray(images)
  ))
  </code></pre>
  
  <p>It works but looks really heavy. Is there a way to define json with code like this ?</p>
  
  <pre><code>val images : List[List[(String, Any)]] = fetchImageUrls(url).map((url: String) =&gt; {
    List(
      "link_path" -&gt; url,
      "display_name" -&gt; "image",
      "size" -&gt; "",
      "modified" -&gt; "",
      "thumbnail" -&gt; url,
      "filename" -&gt; "image",
      "is_dir" -&gt; false,
      "thumb_exists" -&gt; true)
  })
  
  val jsonAst = List(
    "client" -&gt; "urlimages",
    "view" -&gt; "thumbnails",
    "contents" -&gt; images
  ).toJson
  </code></pre>
  
  <p>It doesn't work saying that</p>
  
  <pre><code>Cannot find JsonWriter or JsonFormat type class for List[(String, Object)]
      ).toJson
        ^
  </code></pre>
  
  <p>Which I get, type of each field is not defined at compile time. But why wouldn't it work if serializer does pattern matching anyway ?</p>
  
  <p>Thanks!</p>