Input
New line delimited JSON, sample:
{"summersolstice":"21st June, 2019", //complex objects, arrays, etc ... }
{"summersolstice":"21st June, 2018", //complex objects, arrays, etc ... }
{"summersolstice":"21st June, 2017", //complex objects, arrays, etc ... }
Constraint
While I understand the best way is to be using the shiny new JSON item reader devs released last summer (link), it isn't feasible to update the batch version to the latest one yet. That is the only constraint.
Present Approach
As of now, I followed this stack answer, but I don't think having a T
for FlatFileItemReader
as Map<String, Object>
is the best strategy!
As of now I just take it in with this code:
public class JsonItemReader extends FlatFileItemReader<Map<String, Object>> {
public JsonItemReader(File file) {
Resource resource = new BzipLazyResource(file); //on the fly unzipping
setResource(resource);
setLineMapper(new JsonLineMapper());
}
public JsonItemReader(String sourceFileName) {
this(new File(sourceFileName));
}
}
...and then parse it simply in an ItemProcessor, like:
public class JsonItemProcessor implements ItemProcessor<Map<String, Object>, List<Json>> {
private ObjectMapper mapper = new ObjectMapper();
private static final Logger logger = LoggerFactory.getLogger(JsonItemProcessor.class);
public List<Json> process(Map<String, Object> jsonItem) throws Exception {
JsonNode jsonNode = mapper.valueToTree(jsonItem);
return parseJsonItems(jsonNode);
}