Decode a nested array with circe-optics

172 views Asked by At

I have JSON like this:

  "data": {
    "project": {
      "activityChildren": [
        {
          "id": 2,
          "parents": [
            {
              "id": 1
            }
          ]
        },

      ]
    }
  }

I'd like to decode this to List[(Long, List[Long])] with circe-optics. I got as far as:

  val activityParents: Map[Long, List[Long]] = root.data.activityChildren.each.json
      .getAll(json)
      .flatMap { activity =>
        root.id.long.getOption(activity).map(_ -> root.parents.each.long.getAll(activity))
      }
      .toMap

I wonder whether it's possible to define a single lens for this that just turns the JSON into the desired map without explicitly mapping over the intermediate array. If so, how?

0

There are 0 answers