2

I have a large JSON file of approx 65 MB in below format

{
"Root Node": {
    "Node1": {
        "Node1.1": [{
            "logLevel":"INFO"
            "count" : 20
        },{
            "logLevel":"DEBUG"
            "count" : 200
        },{
            "logLevel":"ERROR"
            "count" : 2000
        }],
        "Node1.2": "",
        "Node1.3": {
            "fromDate": "2014-11-11T14:59:59",
            "toDate": "2014-11-11T14:00:00"
        }
    }
}

}

I am using Jackson to parse the large file I want to read the node Node1.3 then read Node1.1 and create an POJO object which will have all the data from Node1.1 Array and with each data the fromDate and toDate will be associated. I can parse the file if i go in a sequential manner but the Node1.3 is read at the end.

I am Using Below Code to parse the File

public class TempMain {

public static void main(String [] args) throws IOException, ParseException {
    JsonFactory jfactory = new JsonFactory();
    ObjectMapper mapper = new ObjectMapper();

    try(JsonParser jParser = jfactory.createParser(new File("/tmp/file.json"))) {
        // loop until token equal to "}"
        while (jParser.nextToken() != com.fasterxml.jackson.core.JsonToken.END_OBJECT) {
            String fieldname = jParser.getCurrentName();
            jParser.nextFieldName();

            if ("Node1.1".equals(fieldname)) {
                /** current token is "[", move next messages is array, loop until token equal to "]"
                 **/
                jParser.nextToken();
                while(jParser.nextToken() == com.fasterxml.jackson.core.JsonToken.START_OBJECT) {
                    // read everything from this START_OBJECT to the matching END_OBJECT
                    // and return it as a tree model ObjectNode
                    ObjectNode node = mapper.readTree(jParser);

                    // Logic to process the data

                }
            }
        }
        jParser.close();
    } catch(Exception e) {
       e.printStackTrace();
    }
}

}

4
  • Is Node Name in the "key" sorted? Commented May 18, 2018 at 8:39
  • @NeerajJain No its not sorted Commented May 18, 2018 at 8:43
  • Since Node1 is the key of Map<String, Object>, So you can tweak the logic a little bit to achieve what you desire. Set keys = Node1.keySet(); keys.toArray()[keys.size]; // this line will give you the last key in your Map Commented May 18, 2018 at 8:55
  • @NeerajJain I have updated the Question With how I am parsing the file, Also since it's a large JSON i cannot load the data in memory Commented May 18, 2018 at 9:20

1 Answer 1

1

JSON is a forward-reading format. Unless you don't care any sort of verification that the data you're reading are really the ones you expected rather than a completely different object named completely differently, then it is not possible to read it from the end.

Arrange to receive JSON files that are organized with the data you need first. Install a pre-parsing of the files you already have to rewrite them so that they use the order you need. That way, at the moment it will matter, you'll be able to read the file efficiently.

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

1 Comment

This Can be the Solution but the file which i am processing is external File and Arranging the file again in will be an overhead

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.