0

I am creating a spring batch app which accepts a filename from JobParameters and use it to read a json file in a certain path.

I tried creating a JsonItemReader, here is the below code...

@Bean
@StepScope
public JsonItemReader<MyObject> myJsonItemReader() {

    final ObjectMapper mapper = new ObjectMapper();

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

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

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

}

And here is the sample json i am going to read.

   {                                                                                                
        "attr1":"abc",                                                                                          
        "list":[                                                                                            
            {                                                                                       
                "attr_list1": "abc",                                                                                    
                "attr_list2":"abc"                                                                                                                                                  
            },                                                                                      
            {                                                                                       
                "attr_list1": "abc",                                                                                    
                "attr_list2":"abc",                                                                                                                                                                 
            }                                                                                       
        ]   
    }

                                                                                
                                                                                            
                                                                                            

But upon running and debugging my reader, i got an error "The Json input stream must start with an array of objects"

1 Answer 1

2

The JsonItemReader expects an array of JSON objects as input, as mentioned in its Javadoc:

ItemStreamReader implementation that reads Json objects from a Resource having
the following format:

[ { // JSON object }, { // JSON object } ]

Your input is not conform to the expected format, so you need to adapt your input or use another item reader.

I would like to mention that a single json object is not suitable to use as input to a batch process. I believe the input should be an array of objects so that the item reader returns items one by one as designed to work with the chunk-oriented processing model offered by Spring Batch (and other frameworks/tools). The ndjson is also a good choice, but certainly not a single object.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response, as for the single json object (it is my clients decision) but what i did was pass the json objects not included in the list as JobParameter and i write the array of json object in a json file and read it using JsonItemReader.
ok if you found a solution to adapt the input, that's fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.