Skip to content

Instantly share code, notes, and snippets.

@gekomad
Created October 16, 2019 11:13
Show Gist options
  • Save gekomad/4b605206d2ce56f19c1be8946fb8e27f to your computer and use it in GitHub Desktop.
Save gekomad/4b605206d2ce56f19c1be8946fb8e27f to your computer and use it in GitHub Desktop.
Scala Circe DropNull
def _reduce(l: Seq[(String, Json)]): Json = l match {
case ::(x, xs) =>
if (x._2.isNull) {
dropNull(Json.fromFields(xs))
} else {
val jj = Json.fromJsonObject(JsonObject((x._1, dropNull(x._2))))
xs match {
case Nil => jj
case _ => dropNull(Json.fromFields(xs)).deepMerge(jj)
}
}
case Nil => Json.Null
}
def dropNull(j: Json): Json = j.asObject match {
case None =>
j.asArray match {
case Some(value) =>
Json.fromValues(value.map { a =>
a.asObject.map(_.toList match {
case x :: xs =>
_reduce(x :: xs)
case Nil => Json.Null
})
dropNull(a)
})
case None => j
}
case Some(value) =>
value.toList match {
case Nil => j
case l => _reduce(l)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment