0

I'm currently trying to merge multiple .txt files into one big json file. I created a function that takes in the txt file as an argument and the json file it will write too. I'm utilizing google's simple-json package to handle creation of json objects.

The main issues I'm having is that it writes succesfully to the json file, but is not properly formatted.

For example, this is one line of the text file

{"sensor_name":"Activity","timestamp":{"start_time":"Thu Jan 5 00:00:00 EST 2017","end_time":"Thu Jan 5 07:57:33 EST 2017"},"sensor_data":{"activity":"unknown","duration":28653345}}

My approach is to loop over the file, convert each line into a JSONObject type and then write that JSONObject to the file. The code successfully writes the JSONObject to the file, but fails to format the .json properly.

Here is an example of what the .json will look like.

{"sensor_data":{"duration":41143403,"activity":"unknown"},"sensor_name":"Activity","timestamp":{"start_time":"Mon Mar 6 00:00:00 EST 2017","end_time":"Mon Mar 6 11:25:44 EST 2017"}}
{"sensor_data":{"duration":61245,"activity":"walking"},"sensor_name":"Activity","timestamp":{"start_time":"Mon Mar 6 11:25:44 EST 2017","end_time":"Mon Mar 6 11:26:45 EST 2017"}}
{
  {
    "sensor_data":{
    "duration":41143403,
    "activity":"unknown"
  },
    "sensor_name":"Activity",
    "timestamp":{
      "start_time":"Mon Mar 6 00:00:00 EST 2017",
      "end_time":"Mon Mar 6 11:25:44 EST 2017"
  },
},
  {
    "sensor_data":{
    "duration":61245,
    "activity":"walking"
  },
    "sensor_name":"Activity",
    "timestamp":{
      "start_time":"Mon Mar 6 11:25:44 EST 2017",
      "end_time":"Mon Mar 6 11:26:45 EST 2017"
    }
  },
},

the important details to keep in mind is adding commas at the end of each object. The parser nor the .toString method will not handle it

/*
* method to set up a txt file as a json file
* each line in the text file is considered an object
* Args[]
*   txtFile (File): text file that is not formatted in json
*   fileToWriteTo (FileWriter): the file that the json will be written to
* returns
*   none
* */
private void prepareForJson(File txtFile, FileWriter fileToWriteTo) {
    // parser for reading strings
    JSONParser parser = new JSONParser();
    BufferedReader reader;
    try {
        reader = new BufferedReader((new FileReader(txtFile)));
        String line = reader.readLine();

        // convert that line to an json object
        while (line != null) {
            JSONObject lineConvertedToJson = (JSONObject) parser.parse(line);
            // write to the file
            fileToWriteTo.write(lineConvertedToJson.toJSONString());
            // read the next line
            line = reader.readLine();
        }
    } catch (Exception e){
        e.getStackTrace();
    }
}

Any suggestions on how to properly format txt files into json files?

AfternoonTiger
  • 357
  • 1
  • 4
  • 10
  • 1) your elements must be inside an array: `[{"sensor_data":{}}, {"sensor_data":{}}]` 2) you can try something like: `'[' + String.join(",", lines) + ']'` and parse that 3) you may want to look at [this](https://stackoverflow.com/questions/1605332/java-nio-filechannel-versus-fileoutputstream-performance-usefulness) if we are talking about a lot of files – vault Sep 14 '19 at 17:15
  • before this, I created an JSONArray object and would convert the line to a JSONObject and then user JSONArray.add(JSONObject) and then just use parser.parse(JSONArray.toString) to write it to the file, but it still did not work. – AfternoonTiger Sep 14 '19 at 17:18
  • Cool, I will try the above^ – AfternoonTiger Sep 14 '19 at 17:21
  • Not specific to your JSON library, but check out this question: https://stackoverflow.com/questions/4105795/pretty-print-json-in-java – Slaw Sep 14 '19 at 17:51

1 Answers1

0

If the .txt file contains the same structured json contents, you can read each .txt file, create a string with opening bracket([) then put the contents in a string, append the string with a comma(,). Finally put the closing bracket (]) and write the whole string to a file. Even if there is a extra comma, after the end of the json object but before the array, it means before closing bracket, it is fine. You check the whole contents of json in http://jsonviewer.stack.hu/ site. This approach does not require json parsing. However, you can still use Json parsing by reading each file and creating an array of json object and finally you write into a file.

Sambit
  • 7,625
  • 7
  • 34
  • 65
  • Does Jsonviewer (link you provided) let you know if you json you copied and pasted is properly formatted or will it just automatically fix it so you can just view it in on the page? – AfternoonTiger Sep 14 '19 at 17:50
  • jsonviewer link does not format automatically, there is a button to format it. This is a useful link for json validation. Besides, if you want to format it, you have to parse it, you have to use Jackson ObjectMapper and then use the method mapper.defaultPrettyPrintingWriter().writeValueAsString(). – Sambit Sep 14 '19 at 18:19