jsonscalaspray-json

Spray JSON parse nested JSON Array


I have a case class with string list fields and I can't achieve to parse it from JSON. I've defined a JSON Reader :

  val jsonReader = new JsonReader[PeaceWatcherReport] {
    override def read(json: JsValue): PeaceWatcherReport = {
      val fields = json.asJsObject.fields;
      new PeaceWatcherReport(
        fields("id").convertTo[String],
        LocalDateTime.parse(fields("date").convertTo[String]),
        fields("location").convertTo[String],
        JsArray(fields("surroundingCitizens")).convertTo[Seq[String]].toList,
        JsArray(fields("heardWords")).convertTo[Seq[String]].toList
      );
    }
  }

And I user it as follows :

val report : PeaceWatcherReport = JsonParser(record.value().toString).convertTo[PeaceWatcherReport](PeaceWatcherReport.jsonReader);

I get the following error :

Expected String as JsString, but got ["0cb9f193-792b-4b99-a4f3-f7510f10d62b","673229b2-4b ba-4faf-a0cf-30cc58ae9c6c","65148ea2-5140-401f-99db-7d4c1bf2d636","3f1a6c1c-a527-40a8-a690-9f6db701680c","8624e4dd-2f86-4f2a-ac81-d18b 5d7601d3"]

What am I doing wrong ?

Thank you in advance


Solution

  • In fact I didn't need to use JsonArray since it already was one :

      val jsonReader = new JsonReader[PeaceWatcherReport] {
        override def read(json: JsValue): PeaceWatcherReport = {
          val fields = json.asJsObject.fields;
          new PeaceWatcherReport(
            fields("id").convertTo[String],
            LocalDateTime.parse(fields("date").convertTo[String]),
            fields("location").convertTo[String],
            fields("surroundingCitizens").convertTo[Seq[String]].toList,
            fields("heardWords").convertTo[Seq[String]].toList
          );
        }
      }