Chetan Nakum
Chetan Nakum

Reputation: 433

JSON not print as Pretty Print using GSON in Scala

My code is as below :

 var json_val = (Json(DefaultFormats).write(map))

The above json value is like as below :

{"Tatyana Nader":[{"source":"Marseille Rail Station (XRF)","amount":"5000.0","points":"500","date_time":"Sun Dec 06 07:36:09 IST 2015","class":"First","destination":"Batna Arpt (BLJ)"}]}

I want to print this json data as beautify JSON / Preety Json.

So I have used gson for that as below :

var gson:Gson = null
gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create()
var jp: JsonParser = null
var je: JsonElement = null
je = jp.parse(json_val)
var json: String = gson.toJson(json_val)
var prettyJsonString: String = gson.toJson(je)

I have tried lot's of time but it's returning Null Pointer Exception every time. Please advice on it.

Thank you in advance.

Upvotes: 0

Views: 193

Answers (1)

Nagarjuna Pamu
Nagarjuna Pamu

Reputation: 14825

if you are using Play Json

import play.api.libs.json.Json

val jsonString = """{"Tatyana Nader":[{"source":"Marseille Rail Station (XRF)","amount":"5000.0","points":"500","date_time":"Sun Dec 06 07:36:09 IST 2015","class":"First","destination":"Batna Arpt (BLJ)"}]}"""

Json.prettyPrint(Json.parse(jsonString)).toString

Upvotes: 1

Related Questions