scala splitting json into multipe objects if array valued filed is found -
i need split json object in scala multiple objects if array valued fields found.
input json
{ "name" : "test" "age" : 22, "courses" : [math, english] }
should split into
[{ "name" : "test" "age" : 22, "courses" : "math" }, { "name" : "test" "age" : 22, "courses" : "english" }]
are there frameworks support kind of splitting. thanks!
its best if model json scala object , work it. in example using jackson library , addon module scala. use appropriate json library. here modelling json case class using convenient json case class deserialization ability provided jackson scala module
import com.fasterxml.jackson.databind.objectmapper import com.fasterxml.jackson.module.scala.defaultscalamodule import com.fasterxml.jackson.module.scala.experimental.scalaobjectmapper val mapper = new objectmapper() scalaobjectmapper mapper.registermodule(defaultscalamodule) case class a(name: string, age: int, courses: array[string]){def tojson = mapper.writevalueasstring(courses.map(e => a(name,age,array(e))))} val json = """{ "name" : "test", "age" : 22, "courses" : ["math", "english"] }""" val ana = mapper.readvalue(json, classof[a]) ana.tojson
Comments
Post a Comment