Search code examples
jsonscalaplayframeworkplay-json

Errors: {"obj.overallCount":[{"msg":["error.path.missing"],"args":[]}]}


I am trying to convert the json in to Data class. It is showing an error

Errors: {"obj.overallCount":[{"msg":["error.path.missing"],"args":[]}]}

, I am not being able to figure out the problem. Looking for experts' help:


case class Count(count:Long)
  case class OverallCount(overallCount:List[Count])

    "JsonTest" should "show result" in {
      Future {


        import play.api.libs.json._

        implicit val count = Json.reads[Count]
        implicit val ovc = Json.reads[OverallCount]

        val jsonString: JsValue = Json.parse(
          """{"countOverall":[{"count":4}]}"""
        )


        val residentFromJson: JsResult[OverallCount] = Json.fromJson[OverallCount](jsonString)


        residentFromJson match {
          case JsSuccess(r: OverallCount, path: JsPath) => Console.println("overallCount: " + r.overallCount)
          case e: JsError => println("Errors: " + JsError.toJson(e).toString())
        }


        succeed
      }
    }

Solution

    • Either replace

      val jsonString: JsValue = Json.parse(
        """{"countOverall":[{"count":4}]}"""
      )
      

      with

      val jsonString: JsValue = Json.parse(
        """{"overallCount":[{"count":4}]}"""
      )
      
    • or replace

      case class OverallCount(overallCount: List[Count])
      

      with

      case class OverallCount(countOverall: List[Count])