Library to create a JSON file from an analyzed text document


I parsed a timetable from a text document and reached the step where I have to create a JSON file from the raw data like in the sample below. Is there any library which could help me create a JSON formatted file from a raw text document?

I appreciate any help.

Sample how it could look like:

   {"route": 1
     "info": [
               {"direction": "Surrey Quays"},
               {"stops": [{"stops_name": "Lancaster Place"},
                                       "mon-fri": ["04:41", "05:41", "06:09"],
                                       "sat": [ "05:38", "06:07","06:37"]


Some sample from the text document

Surrey Quays
Lancaster Place

mon-fri 04:41 05:41 06:09
sat  04:41 05:41 06:09


            for (Entry<String, List<String>> entry : map.entrySet()) {
            String key = entry.getKey();
            List<String> timeEntries = entry.getValue();
            JSONObject timeTable = new JSONObject();
            timeTable.put("route", route);

            JSONObject info = new JSONObject();
            info.put("direction", direction);

            JSONObject stops = new JSONObject();
            stops.put("stops_name", key);

            JSONObject arrivals = new JSONObject();
            JSONArray arrivalMoFr  = new JSONArray();

            JSONArray someArray = new JSONArray(timeEntries);
            arrivalMoFr.put( someArray);

            arrivals.put("mon-fri", arrivalMoFr);

            stops.put("arrival_time", arrivals);

            info.put("stops", stops);
            timeTable.put("info", info);



**Some of the result **

 "arrival_time": {"mon-fri": [[

You could use this JSON library from . But it is just one example of all the libraries out there.

This is how you could use it: Let's say you already have your parser (meaning you already have a method that can read the text document and that knows what to do with the data)

JSONObject timeTable = new JSONObject(); // first create the "main" JSON Object
timeTable.put("route", 1);               // this first entry is just a number
JSONObject info = new JSONObject();      // now you need a second JSON OBject to put all the info in
info.put("direction", "Surrey Quays");   // first info, the direction

JSONObject stops = new JSONObject();     // now you need another Object for all the stops and stop related info
stops.put("stops_name", "Tension Way");  // like the name for the stop

JSONObject arrivals = new JSONObject();  // now all the arrivals
JSONArray arrivalMoFr = new JSONArray(); // this is the first time where an array makes sense here
arrivalMoFr.put("05:38");                // arrival time 1
arrivalMoFr.put("06:07");                // arrival time 2
arrivalMoFr.put("06:37");                //  ...
arrivals.put("mon-fri",arrivalMoFr);     // add the arrival times array as mon-fri to the arraivals object, do the same with all other arrival times (Saturday, Sunday...)
stops.put("arrival_time", arrivals);     // now add the arrival time object to your stops
info.put("stops", stops);                // and put the stops to your info
timeTable.put("info", info);             // once you added all your infos you can put the info into your timeTable

System.out.println(timeTable.toString(3));  // this is just for testing. The number 3 tells you how many whitespaces you want for text-identaion

And this is the output I am getting:

   "route": 1,
   "info": {
      "stops": {
         "arrival_time": {"mon-fri": [
         "stops_name": "Tension Way"
      "direction": "Surrey Quays"

I realize this is not exactly the same as in your example. But I think you get the idea. Remember, the order of the elements in a JSON Object is irrelevant. When reading the JSON, the program will read it like json.get("stops_name"); and it does not care if the stops name are before or after the arrival times.


I saw that you put the "stops_name" and the "arrival_time"-array in separate JSON Objects. Well if you want to put them all in an object called "stops", which suggests, that you will list more than one stop, I suggest to put them together. Because the data in the JSON object has no particular order.

"stops": [
      "stop_name" : "Lanvester Place",
      "arrival" : {...}
      "stop_name" : "Some other Place",
      "arrival" : {...}

To your EDIT:

your are getting double braces [[ because you are adding an array to your arrivals Object where the first entry is again an array.

so instead of doing this:

    JSONArray arrivalMoFr  = new JSONArray();

    JSONArray someArray = new JSONArray(timeEntries);
    arrivalMoFr.put( someArray);

    arrivals.put("mon-fri", arrivalMoFr);

you should do this:

    JSONArray arrivalMoFr  = new JSONArray(timeEntries);  // put your list of time entries directly into the arrivalMoFr array
    arrivals.put("mon-fri", arrivalMoFr);  // then add the arrivalMoFr array to the arrivals object