title: Spray-Json: How to parse a Json Array?
body: <p>I'm new to the Spray-Json API and I'm trying to parse a Json response from the Docker REST API. </p>
  
  <p><a href="https://github.com/spray/spray/blob/master/examples/spray-client/simple-spray-client/src/main/scala/spray/examples/Main.scala">There</a> is a clean example of the usage of Spray-Json to parse this Google Map Json response :</p>
  
  <pre><code>{
     "results" : [
        {
           "elevation" : 8815.7158203125,
           "location" : {
              "lat" : 27.988056,
              "lng" : 86.92527800000001
           },
           "resolution" : 152.7032318115234
        }
     ],
     "status" : "OK"
  }
  </code></pre>
  
  <p>In the above example the outermost level is an <code>Object</code>. However, I need to directly parse a Json response whose outermost level is an <code>Array</code> composed of containers information as shown below :</p>
  
  <pre><code>[
       {
         "Id": "8dfafdbc3a40",
         "Image": "base:latest",
         "Command": "echo 1",
         "Created": 1367854155,
         "Status": "Exit 0",
         "Ports":[{"PrivatePort": 2222, "PublicPort": 3333, "Type": "tcp"}],
         "SizeRw":12288,
         "SizeRootFs":0
       },
       { ... },
       { ... }
  ]
  </code></pre>
  
  <p>Here is a code that I adapted from the Google map example :</p>
  
  <pre><code>package main
  
  import ...
  
  case class Container(id: String, image: String, command: String, created: Long, status: String, ports: List[Port], sizeRW: Long, sizeRootFs: Long)
  case class Port(privatePort: Long, publicPort: Long, portType: String)
  case class DockerApiResult[T](results: List[T])
  
  object ContainerListJsonProtocol extends DefaultJsonProtocol {
    implicit val portFormat = jsonFormat3(Port)
    implicit val containerFormat = jsonFormat8(Container)
    implicit def dockerApiResultFormat[T :JsonFormat] = jsonFormat1(DockerApiResult.apply[T])
  }
  
  object Main extends App {
  
    implicit val system = ActorSystem("simple-spray-client")
    import system.dispatcher // execution context for futures below
    val log = Logging(system, getClass)
  
    log.info("Requesting containers info...")
  
    import ContainerListJsonProtocol._
    import SprayJsonSupport._
    val pipeline = sendReceive ~&gt; unmarshal[DockerApiResult[Container]]
  
    val responseFuture = pipeline {
      Get("http://&lt;ip-address&gt;:4243/containers/json")
    }
  
    responseFuture onComplete {
      case Success(DockerApiResult(Container(_,_,_,_,_,_,_,_) :: _)) =&gt;
        log.info("Id of the found image: {} ")
        shutdown()
  
      case Success(somethingUnexpected) =&gt;
        log.warning("The Docker API call was successful but returned something unexpected: '{}'.", somethingUnexpected)
        shutdown()
  
      case Failure(error) =&gt;
        log.error(error, "Couldn't get containers information")
        shutdown()
    }
  
    def shutdown(): Unit = {
      IO(Http).ask(Http.CloseAll)(1.second).await
      system.shutdown()
    }
  }
  </code></pre>
  
  <p>And below is the exception I get (<code>Object expected</code>) :</p>
  
  <pre><code>spray.httpx.PipelineException: MalformedContent(Object expected,Some(spray.json.DeserializationException: Object expected))
  </code></pre>
  
  <p>I certainly miss something obvious but <strong>How to parse a Json Array using Spray-Json?</strong></p>
  
  <p>Also, is there a simple way to do this without having to deal with custom JsonFormat or RootJsonFormat?</p>