I am given js1 as a string.
I want to nest "a
", "b
", "c
" under "abc
".
I feel like this can be done a few lines of code. What is a better way to do this below?
val js1 = """
{
"name" : "test",
"a" : true,
"b" : true,
"c" : true,
"d" : true,
"f" : true,
"g" : true,
"h" : true,
}
"""
val jsGroups = parse(js1)
val a = (jsGroups \ "a").values.toString.toBoolean
val b = (jsGroups \ "b").values.toString.toBoolean
val c = (jsGroups \ "c").values.toString.toBoolean
val abc = ("a" -> a) ~ ("b" -> b) ~ ("c" -> c)
val r = jsGroups.remove { x =>
x match {
case JField("a", bool) => true
case JField("b", bool) => true
case JField("c", bool) => true
case _ => false
}
}
val newJs = r.merge(JObject(List(JField("abc", abc))))
println(pretty(render(newJs)))
output must be
{ "name":"test", "d":true, "f":true, "g":true, "h":true, "abc":{ "a":true, "b":true, "c":true } }
The simplest way is to use a
case class
.