davidmrz
davidmrz

Reputation: 431

Scala Play parse json stream (ndjson)

I have an endpoint that produces application/stream+json or application/x-ndjson (http://ndjson.org/).

I'm trying to consume this endpoint with Play. Specifically I've used WSResponse.json but it only seem to parse the first item returned. I also tried WSResponse.validate[Seq[JsValue]] but it fails with JsonValidationError.

Is it possible to parse this output with Play and get a Seq or even a Source (akka-streams)?

Upvotes: 1

Views: 477

Answers (1)

davidmrz
davidmrz

Reputation: 431

I managed create a solution based on cchantep's comment

Using akka-streams framing, specifically the class JsonFraming will do the trick. I ended up with something like this:


// declaration or injection of WSClient

ws.url(url)
  .post(body)
  .flatMap { response =>
    for {
      jsonStrs <- response.bodyAsSource.via(JsonFraming.objectScanner(Int.MaxValue))
        .runFold(Seq.empty[String]) {
          case (acc, entry) => acc ++ Seq(entry.utf8String)
        }
    } yield {
      jsons.map(Json.parse)
    }
  }

The code above will produce a Future[Seq[JsValue]] which can be manipulated as needed.

Upvotes: 2

Related Questions