How to Read Large Json File in chunks

Viewed 582

I am using below JSON format:

[{object1},{object2},{object3}...]

I am reading a JSON file object wise means one by one (object by object) and my below code working fine.

I want to read it in chunks(at a time 10 objects). I have tried a lot but I am not getting any solution (without using Spring Batch). Can anybody please help me how to read in chunks

@Component
public class PdpiRunner3 implements CommandLineRunner {

    
    @Override
    public void run(String... args) throws Exception {
        
            try {
            JsonReader jsonReader = new JsonReader(new InputStreamReader(new ClassPathResource("/json/trades2.json").getInputStream(),StandardCharsets.UTF_8));
            
            Gson gson = new GsonBuilder().create();
            jsonReader.beginArray();
            while (jsonReader.hasNext()) { // next json array element
                PDPIfiles json = gson.fromJson(jsonReader, PDPIfiles.class);
                if (json != null) {
                    System.out.println(json);
                }

            }
            jsonReader.endArray();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

Is it possible to read json element In Chunk without spring Batch pleas help ?

0 Answers
Related