I have a complex JSON below. I am reading it using FlatFileItemReader. How can I ignore the last line "]", with my customized ComplexJsonRecordSeparatorPolicy?
[
{"firstName":"Tom", "lastName":"Cruise"},
{"firstName":"Bruce", "lastName":"Willis"},
{"firstName":"Liam", "lastName":"Neeson"}
]
My ComplexJsonRecordSeparatorPolicy looks like below. This class is successfully working, when I have "]" in line no 4 but, it throws an error when the line is supplied with only "]" in line no 5, as my post processor deletes the line instead of ignoring it.
public class ComplexJsonRecordSeparatorPolicy extends JsonRecordSeparatorPolicy {
@Override
public boolean isEndOfRecord(String line) {
return StringUtils.countOccurrencesOf(line, "{") == StringUtils.countOccurrencesOf(line, "}")
&& (line.trim().endsWith("}") || line.trim().endsWith(",") || line.trim().endsWith("]"));
}
@Override
public String postProcess(String record) {
if (record.startsWith("["))
record = record.substring(1);
if ((record.endsWith("]") || record.endsWith(",")))
record = record.substring(0, record.length() - 1);
return super.postProcess(record);
}
}