Flattern json data write in Scala file

#1

This is my sample:

“clHd”: {
“adjudCode”: “64”,
“adjusNumr”: 20,
“fixCode”: “ABC”,
“assignedNumr”: “93612”,

Output
“clHd_adjudCode” :64,
“clHd_adjusNumr”: 20,
“clHd_fixCode”: “ABC”,
“clHd_assignedNumr”: “93612”

Request : I’m flattening the Json file and finally trying to write into the file

Below is transform the flatten and just print in the screen:
jsonParsed.transform(claimHeader).foreach(Json.stringify _ andThen println))

I have to write into the file(using scala) how will i write into above statement.

I tried this.But throw the error.Not enough declaration.
val s = new PrintWriter(“C:/Users/Rajesh/Desktop/Json_schema_output.txt”)
s.write(jsonParsed.transform(claimHeader).foreach(Json.stringify _ andThen println)

Please advise

#2

What’s the exact error message? Could you post a small but complete code sample?

foreach returns Unit, and therefore it does not make sense to put an expression that ends with foreach as an argument to write.

#3

object JsonExample {
def main(args: Array[String]) {
val clHder = (__ \ 'clHder).read[JsObject].flatMap(
.fields.foldLeft((_ \ 'clHder).json.prune) {
case (acc, (k, v)) => acc andThen .json.update(
Reads.of[JsObject].map(
+ (s"clHder
$k" -> v))
)
})

val source: String = Source.fromFile(“C:/Desktop/Json_schema.json”).getLines.mkString
val jsonParsed: JsValue = Json.parse(source)

How to use this in my below file write.I didn’t face any kind of error
println(jsonParsed.transform(clHd).foreach(Json.stringify _ andThen println))

val s = new PrintWriter(“C:/Desktop/Json_schema_output.txt”)

s.write()
s.close()

I have to write into the file.I don’t have idea how to mention the above

#4

I got it.Resolved my issue.Thanks for the hint.