maloney maloney - 29 days ago 7
JSON Question

Play ScalaJSON Reads[T] parsing type mismatch error

I have a big chunk of Json, of which I only care about a small subset e.g:

{
"request": {
"c": {
"code": "AA",
"fsCode": "AA"
},
"codeType": {},
"number": {
"requested": "100",
},
"scheduled": [
{
"code": "BB",
"number": "100",
"classes": [
"F",
"Y"
]
}]
}


I just want to extract the
code
and
number
variables. I have tried using
Reads
as follows:

case class TheResult(result: (String, String))

implicit val theScheduledReads: Reads[TheResult] = (
(__ \ "code").read[String] and
(__ \ "number").read[String] tupled) map TheScheduledFlight.apply _

case class ResultList(addr: List[TheResult])

implicit val resultReads: Reads[ResultList] =
(__ \ "scheduled").read(List[TheResult](theScheduledReads)) map ResultList _


and then this to access the result:

response.json.validate[List[ResultList]]


However, it throws this error:

type mismatch;
found : play.api.libs.json.Reads[details.client.TheResult]
required: details.client. TheResult
(__ \ "address").read(List[TheResult](resultReads)) map (TheResult.apply _)


Any ideas what I'm doing wrong?

Answer

Fixed multiple problems in the code. Have a look

Most importantly read(List[TheResult](theScheduledReads)) makes no sense. It should be read[List[TheResult]]

Replace TheScheduledFlight.apply with TheResult.apply _

case class TheResult(result: (String, String))

implicit val theScheduledReads: Reads[TheResult] = (
  (__ \ "code").read[String] and
    (__ \ "number").read[String] tupled) map TheResult.apply _ //problem fixed here

case class ResultList(addr: List[TheResult])

implicit val resultReads: Reads[ResultList] =
  (__ \ "scheduled").read[List[TheResult]] map ResultList.apply _ //problem fixed here
Comments