Reputation: 1513
I'm looking for a most elegant way to traverse a list of objects with multiple fields in Json with circe optics.
Let's say we have this sort of JSON:
[
{
"key1": "one",
"key2": "two"
},
{
"key1": "three",
"key2": "four"
}
]
and we have a case class case class Entity(key1: String, key2: String)
So I want to find the most elegant and sleek way to traverse this JSON and create a list of case objects in the end.
I know that I can use each: root.each.key1.string.getAll(json)
, but how would I build a lens that will give me a traversable tuple (?) or something that I could put into for comprehension. I can probably combine lenses somehow.
There's already a question like that (how to parse un Array of object with Circe) but it has only one field in each object.
Upvotes: 0
Views: 637
Reputation: 2253
Recently I was also trying to solve a very similar problem: I wanted to implement with circe-json
what would be the equivalent of the following jq
line:
cat json | jq '.[] | {key1: .key1, key2: .key2}'
The closest I got was this:
import io.circe.optics.JsonPath.root
root.each.json.getAll(json)
.map(j => (root.key1.string.getOption(j).get, root.key2.string.getOption(j).get))
.map(Entity.apply)
Upvotes: 0