I have a String with many records in JSON Format. I have to convert each JSON record to one-line JSON record.
Example: Input:
{
"field1" : "aa11",
"field2" : "aa22",
"structField" : {
"sf1" : "aaa11",
"sf2" : "aaa22"
}
}, {
"field1" : "bb11",
"field2" : "bb22",
"structField" : {
"sf1" : "bbb11",
"sf2" : "bbb22"
}
}, {
"field1" : "cc11",
"field2" : "cc22",
"structField" : {
"sf1" : "ccc11",
"sf2" : "ccc22"
}
}
Output:
{"field1":"aa11","field2":"aa22", "structField":{"sf1" : "aaa11","sf2" : "aaa22"}},
{"field1":"bb11","field2":"bb22","structField":{"sf1" : "bbb11","sf2" : "bbb22"}},
{"field1" : "cc11","field2" : "cc22","structField" : {"sf1" : "ccc11","sf2" : "ccc22"}}
I am using Scala to try to parse the String and split it by "}, {"
and reformat my JSON:
myMultiJSONString.
substring(2,myMultiJSONString.length-2).
split("\\}, \\{").
map(reg => "{" + reg.trim.replaceAll("\\n","") + "}")
I think this is a dirty way.
Is there some library which can help with this stuff?
For example, deserializing JSON String to "something" and serializing later in one-line JSON String.
Any idea?
Thanks!