jsonscalaplayframeworklagom

JsonValidationError is not an object error interpretation


Does any one have any idea what the cause of this error might be?

Future(
  Success(
    JsError(
      List(
        (,List(
            JsonValidationError(
              List(
                [{"FIELD1":"field1value","FIELD2":"field2value","FIELD3":"field3value"}] is not an object
              ),
              ArraySeq())
))))))

Thanks!

UPDATE The code that generated this output looks like:

val queryResponse: Future[JsResult[MyObject]] = ws.url(queryURL)
    .withCookies(cookies.toSeq :_*)
    .get().map{response =>
    (response.json \ "object").validate[MyObject]
  }

The response coming from the server seems to be a list of JSON objects.

MyObject structure looks like:

case class MyObject(
                    FIELD1: Int,
                    FIELD2: Int,
                    FIELD3: String,
                    FIELD4: String,
                    FIELD5: String,
                    FIELD6: Int,
                    FIELD7: String,
                    FIELD8: String,
                    FIELD9: Option[String],
                    FIELD10: String,
                    FIELD11: String,
                    FIELD12: String,
                    FIELD13: Int
                  )

object MyObject
{

  implicit val format: Format[MyObject] = Json.format

  implicit val myObjectWrites = Json.writes[MyObject]

  implicit val myObjectReads: Reads[MyObject] = (
    (JsPath \ "FIELD1").read[Int] and
      (JsPath \ "FIELD2").read[Int] and
      (JsPath \ "FIELD3").read[String] and
      (JsPath \ "FIELD4").read[String] and
      (JsPath \ "FIELD5").read[String] and
      (JsPath \ "FIELD6").read[Int] and
      (JsPath \ "FIELD7").read[String] and
      (JsPath \ "FIELD8").read[String] and
      (JsPath \ "FIELD9").readNullable[String] and
      (JsPath \ "FIELD10").read[String] and
      (JsPath \ "FIELD11").read[String] and
      (JsPath \ "FIELD12").read[String] and
      (JsPath \ "FIELD13").read[Int])(MyObject.apply _)

}

Solution

  • You tried to parse an object from a JSON document which looked like this:

    {
      "object": [
        { "FIELD1":"field1value","FIELD2":"field2value","FIELD3":"field3value"}
      ]
    }
    

    Instead, use

    (response.json \ "object" \ 0).validate[MyObject]
    

    or

    (response.json \ "object").validate[Seq[MyObject]]
    

    Here is a scastie snippet to illustrate: https://scastie.scala-lang.org/avdv/gNc2SFkIR2ukvDtr7ZiYJw