1

I'm having some trouble using circe for a more complex extraction. If I have the below Json:

{
  "data": [
    {"a": "a-string", "b": "a-string", "c": "a-string"},
    {"a": "a-string", "b": "a-string", "c": "a-string"},
    {"a": "a-string", "b": "a-string", "c": "a-string"}
  ]
}

How could I use circe to get a list of those objects, but only containing the a and b fields?

1 Answer 1

2

Try defining a model which contains only a and b fields like so

case class Element(a: String, b: String)

For example,

import io.circe.generic.auto._
import io.circe.parser._

case class Element(a: String, b: String)
case class Data(data: List[Element])

val raw = """{"data": [{"a": "a-string", "b": "a-string", "c": "a-string"},{"a": "a-string", "b": "a-string", "c": "a-string"}, {"a": "a-string", "b": "a-string", "c": "a-string"}] }"""
decode[Data](raw).getOrElse(throw new RuntimeException)

outputs

res0: Data = Data(List(Element(a-string,a-string), Element(a-string,a-string), Element(a-string,a-string)))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.