0

I am reading large Json file, and I wanna to map these objects in the file and insert them in the db

Json structure

{

"A": [
    {
        "key1": "value1",
        "key2": "value2",
        "B": [
            {
                "id": "34f852c8-fc51-47aa-ba7f-dd659a4bff3f",
                "C": [
                    {
                        "c1": "819685-002",

                    }
                ]
            }
        ]
    },

    {
        "key1": "value1",
        "key2": "value2",
        "B": [
            {
                "id": "34f852c8-fc51-47aa-ba7f-dd659a4bff3f",
                "C": [
                    {
                        "c1": "819685-002",

                    }
                ]
            }
        ]
    }
]
}

The file include multiple blocks of A

So i need to loop three nested loops

For (A){
    for (B){
       FOR (C) {
          // CREATE THE AN OBJECT 
       }
     }
 }

 // Then after the loop insert all objects once
 db.bulk(//List of object created);

The issue it takes 3 minutes to insert 2557 record?

Any enhancement can be done to enhance the looping procedure ?

User
  • 573
  • 3
  • 15
  • 28

2 Answers2

0

I would always create model to map the json string to POJO so that I will have flexiblity to act over them. For example, for the part of your json, I would create POJO as:

class Payload {
    private List<Result> A;

    // getters and setters
}


class Result {
    private String key1;
    private String key2;
    private List<Result2> B;

    // getters and setters
}

Now you can map your json to pojo with Jackson as:

import com.fasterxml.jackson.databind.ObjectMapper;
// . . . 
ObjectMapper mapper = new ObjectMapper();
Payload payload = mapper.readValue(jsonString, Payload.class);

Now you can send the payload or part of payload to db.

If you want to use Gson for mapping, then you take look into this example.

Yogen Rai
  • 2,961
  • 3
  • 25
  • 37
-1

This seems to be a bit of an optimization question. Very similar to this one here. Basically, no there's not really a more efficient way of optimizing the 3 nested for loops, however, analyzing your algorithm structure and re-evaluating how you assign your objects and parse through the JSON itself would be a better attack strategy.

That being said, as others have commented, there are tools such as Jackson that will do this for you.

Here's some links that should help you get started with that:
Jenkov Tutorial for Jackson ObjectMapper
Baeldung Tutorial Jackson Object Mapper
Mykong Tutorial Jackson 2 convert Java Object To/From JSON

sdumont
  • 99
  • 5