FranGoitia
FranGoitia

Reputation: 1993

Reading an inner object with Circe

I'm trying to read an inner object in a json I receive. I need to get this inner object as it is and insert it to Mongo.

{
  "order" : {
    "customer" : {
      "name" : "Custy McCustomer",
      "contactDetails" : {
        "address" : "1 Fake Street, London, England",
        "phone" : "0123-456-789"
      }
    },
    "items" : [
      {
        "id" : 123,
        "description" : "banana",
        "quantity" : 1
      },
      {
        "id" : 456,
        "description" : "apple",
        "quantity" : 2
      }
    ],
    "total" : 123.45
  }
}

Upvotes: 0

Views: 380

Answers (1)

flavian
flavian

Reputation: 28511

Based on the original example, you would roll out a Decoder. I'm not a circe expert, I've just used it the first time yesterday, but I think downField should work.

case class Item(id: String, description: String, quantity: Int)
case class InnerObject(items: List[Item])

object InnerObject {
  implicit val decode: Decoder[InnerObject] = Decoder.instance(c =>
    c.downField("items").as[InnerObject]
  )
}

Upvotes: 1

Related Questions