Roman
Roman

Reputation: 66196

How to parse json-file with spring batch?

I have a list of serialized entities written to a file in json format. I need to read them from the file using spring batch (then process them and write to DB).

I found only Flat file, DB and Xml readers provided with spring batch. What's the right way to parse json?

The approach I'm going to implement is:

1. read the whole file with FlatFile reader
2. parse it with Jaxson in `read` method

I'm completely newbie in spring batch, so I need a piece of advice here. Is this approach correct? Doesn't it violates any spring-batch principles? Or should I use another one?

Upvotes: 2

Views: 9584

Answers (2)

chickenempanada
chickenempanada

Reputation: 23

You can use the JsonItemReader in Spring Batch. In my case, I passed the JSON file name as JobParameter.

Sample JSON content in my JSON file.

{ 
   "obj1": "abc", 
   "obj2": "def" 
}

MyObj Class:

@Data
@Builder
public class MyObject{
    String obj1;
    String obj2;
}

And here is my reader.

@Bean
@StepScope
public JsonItemReader<MyObj> myReader() {

    LOGGER.info(LOG_TEMPLATE,
            getClass().getSimpleName(),
            Thread.currentThread().getStackTrace()[1].getMethodName(),
            "Inside Reader...");

    final ObjectMapper mapper = new ObjectMapper();

    final JacksonJsonObjectReader<MyObj> jsonObjectReader = new JacksonJsonObjectReader<>(
            MyObj.class);
    jsonObjectReader.setMapper(mapper);

    final String filePath = config.getRootfolder() + SEPERATOR + inputInfo.getFileName();

    return new JsonItemReaderBuilder<MyObj>().jsonObjectReader(jsonObjectReader)
            .resource(new FileSystemResource(filePath))
            .name("myReader")
            .build();

}

Upvotes: 0

Benoit Wickramarachi
Benoit Wickramarachi

Reputation: 6226

Your approach would work but I suggest you to use Jackson Streaming API to directly parse the JSON file, instead of having an additional step to read the whole file and then process it.

JsonFactory jsonFactory = new JsonFactory();
JsonParser jp = jsonFactory.createJsonParser(file);

Upvotes: 0

Related Questions