Skip to content

Instantly share code, notes, and snippets.

@alicefuzier
Last active September 4, 2017 11:52
Show Gist options
  • Save alicefuzier/5d5f16f83998ba1b2221b202c48f1eaa to your computer and use it in GitHub Desktop.
Save alicefuzier/5d5f16f83998ba1b2221b202c48f1eaa to your computer and use it in GitHub Desktop.
import cats.syntax.either._
import io.circe._
import io.circe.optics.JsonPath._
import io.circe.parser._
val json = parse(s"""
{
"items": [
{
"identifiers": [
{
"identifierScheme": "antarctica",
"value": "husvik"
},
{
"identifierScheme": "greece",
"value": "spinalonga"
},
{
"identifierScheme": "ireland",
"value": "waterfoot"
}
],
"type": "ghost towns"
},
{
"identifiers": [
{
"identifierScheme": "ocean",
"value": "atlantis"
},
{
"identifierScheme": "regal",
"value": "camelot"
}
],
"type": "mythological places"
}
],
"title": "A letter about loss",
"lettering": "things we lose have a way of coming back to us in the end, if not always in the way we expect",
"subjects": [
{
"label": "Loss and grief",
"type": "Concept"
},
{
"label": "Resurrection and renaming",
"identifiers": [
{
"identifierScheme": "england",
"value": "lundenwic"
},
{
"identifierScheme": "scotland",
"value": "dunedin"
}
],
"type": "cities that were renamed"
}
]
}
""").getOrElse(Json.Null)
val pathToObjectWithIdentifiers = root
.each.filter(j => j.isArray)
.each.filter(j=> j.asObject.exists(obj => obj.contains("identifiers")))
//getting all objects with identifiers works
val objectsWithIdentifiers = pathToObjectWithIdentifiers.obj.getAll(json)
print(objectsWithIdentifiers.size)
//this does not compile
pathToObjectWithIdentifiers.each
.modify((json: JsonObject) => {
json.+:(("canonicalId", Json.fromString("hgsvd")))})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment