I have a folder with thousands of text files with JSON content that I need to read, convert into a POJO and then save into a MySQL database. I intend to use a Spring Batch application.
Here is the issue so far, the research I have done only shows reading multiple CSV files or XML files and no JSON data. Specifically, I need to convert this method for parsing a CSV file into a JSON parser.
@Bean
public FlatFileItemReader<Person> reader() {
FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
reader.setLineMapper(new DefaultLineMapper<Person>() {{
setLineTokenizer(new DelimitedLineTokenizer() {{
setNames(new String[] {"firstname", "lastname", "email", "age"});
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
setTargetType(Person.class);
}});
}});
return reader;
}
This code parses a JSON file:
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("C:\\path\\sample.json"));
The method might be something like this
@Bean
Public FileReader<Person> reader() {
FileReader<Person> reader = new FileReader<Person>();
/**** need help on what to do here ****/
return reader;
}
Also seeing that I am reading all the files in a directory, I am passing the value of that directory in this format
@Value(value="C:\\path\\*.json")
private Resource[] resources;
So I need help on how to use this value (directory for all files) instead of what I showed earlier (single file location)
Object obj = parser.parse(new FileReader("C:\\path\\sample.json"));
private Resource[] resources;contains? does it contain array of each file path?