Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use Circe for Decoding JSON Lists/Arrays in Scala

Tags:

json

scala

circe

I have the code snippet

cursor.downField("params").downField("playlist").downField("items").as[List[Clip]]

Where Clip is a simple case class of strings and numbers. The incoming Json should contain a json object "playlist" with an array of "items" where each item is a clip. So the json should look like

{
  "playlist": {
      "name": "Sample Playlist",
      "items": [
        {
          "clipId":"xyz", 
          "name":"abc"
        },
        {
          "clipId":"pqr", 
          "name":"def"
        } 
      ]
   }
}

With the code snippet above, I'm getting the compile error:

 Error:(147, 81) could not find implicit value for parameter d:     
 io.circe.Decoder[List[com.packagename.model.Clip]]
      cursor.downField("params").downField("playlist").downField("items").as[List[Clip]]

What am I doing wrong? How do you setup decoding for a list/array of simple items using circe?

like image 794
mattmar10 Avatar asked Feb 17 '17 03:02

mattmar10


3 Answers

For the sake of completeness, instead of navigating into the JSON value and then decoding the clips, you could create a custom decoder that includes the navigation in its processing:

import io.circe.Decoder, io.circe.generic.auto._

case class Clip(clipId: String, name: String)

val decodeClipsParam = Decoder[List[Clip]].prepare(
  _.downField("params").downField("playlist").downField("items")
)

And then if you've got this:

val json = """{ "params": {
  "playlist": {
      "name": "Sample Playlist",
      "items": [
        {
          "clipId":"xyz", 
          "name":"abc"
        },
        {
          "clipId":"pqr", 
          "name":"def"
        } 
      ]
   }
}}"""

You can use the decoder like this:

scala> io.circe.parser.decode(json)(decodeClipsParam)
res3: Either[io.circe.Error,List[Clip]] = Right(List(Clip(xyz,abc), Clip(pqr,def)))

I'd probably go a step further and use a custom case class:

import io.circe.generic.auto._
import io.circe.generic.semiauto.deriveDecoder

case class Clip(clipId: String, name: String)
case class PlaylistParam(name: String, items: List[Clip])

object PlaylistParam {
  implicit val decodePlaylistParam: Decoder[PlaylistParam] =
    deriveDecoder[PlaylistParam].prepare(
      _.downField("params").downField("playlist")
    )
}

Now you can just write this:

scala> io.circe.parser.decode[PlaylistParam](json).foreach(println)
PlaylistParam(Sample Playlist,List(Clip(xyz,abc), Clip(pqr,def)))

How you want to split up the navigation and decoding is mostly a matter of taste, though.

like image 186
Travis Brown Avatar answered Nov 28 '22 08:11

Travis Brown


Thanks for the help. I was able to figure it out after stepping away for awhile and coming back with fresh eyes.

I think I was going wrong by using the downArray function.

My solution was to do the following:

override def main(args: Array[String]): Unit = {
    import ClipCodec.decodeClip

    val json = parse(Source.fromFile("playlist.json").mkString).right.get
    val clips = json.hcursor.downField("params").downField("playlist")
                   .downField("items").as[Seq[Clip]]

  }
like image 28
mattmar10 Avatar answered Nov 28 '22 08:11

mattmar10


Circe is looking for an implicitly declared decoder for List[Clip] and cannot find it.

I suspect that you have not defined a decoder either manually or (semi)automatically. You can do both by following the official docs https://circe.github.io/circe/codec.html.

Unfortunately I cannot provide more detail than this because the question is rather vague. I will update my answer when more details are given.

like image 30
acidghost Avatar answered Nov 28 '22 10:11

acidghost