I have my circe Decoder as shown below. I am confident my Sentiment Decoder works correctly so won't include it below.
case class CryptoData(value: String, valueClassification: Sentiment)
implicit val decoder: Decoder[CryptoData] = Decoder.instance { json =>
for {
value <- json.downField("data").get[String]("value")
valueClassification <- json.downField("data").get[Sentiment]("value_classification")
} yield CryptoData(value, valueClassification)
}
my Json looks like this
{
"name" : "Fear and Greed Index",
"data" : [
{
"value" : "31",
"value_classification" : "Fear",
"timestamp" : "1631318400",
"time_until_update" : "54330"
}
],
"metadata" : {
"error" : null
}
}
I simply want value
and value_classification
. As can be seen, those values sit within an array.
I suspect Circe is looking to decode a List[data]
but I don't want to create a case class DataInfo(list: List[Data])
it just doesn't feel right.
You just missed a
downArray
call to parsedata
as the array of objects. Working decoder:Small recomendataion:
I would advise you to define a basic decoder for
CryptoData
, it should just decodeCryptoData
from thedata
object:to:
and if you have some extended JSON, you can just move down to the actual
CryptoData
field using some custom parser and parse object.full code:
testing: