1. Engineering
  2. Computer Science
  3. the whole point of this code was to read json...

Question: the whole point of this code was to read json...

Question details

The whole point of this code was to read json files through different directories in which they were inside a folder called "parm" and populate them in a csv file. Can you write a code to make it more efficent(shorter lines of code)

below is the java code

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.PrintWriter;
import java.util.Arrays;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;

public class start {

   public static void main(String[] args) throws FileNotFoundException {
              
      
//   C:\Users\AG05359\Desktop\bdf-rscz-etl\streamsets
//       System.out.println(Arrays.toString(directories));
       String initDirectory = "C:\Users\AG05359\Desktop\bdf-rscz-etl\streamsets";
       String [] directories = returnDirectories(initDirectory);
       System.out.println(Arrays.toString(directories));

       //17-19
       PrintWriter pw = new PrintWriter(new File("parm.csv"));
StringBuilder sb = new StringBuilder();
sb.append("Subject Name");
sb.append(',');
sb.append("Template Name");
sb.append(',');
sb.append("File Name");
sb.append(',');
sb.append("Property Name");
sb.append(',');
sb.append("Property Value");
sb.append(' ');
  
       for(int i =0;i<directories.length;i++){
//           System.out.println(directories[i]);
           String secondDirectory = "C:\Users\AG05359\Desktop\bdf-rscz-etl\streamsets\"+directories[i];
           String [] second_directories = returnDirectories(secondDirectory);
           System.out.println(Arrays.toString(second_directories));
           for(int j = 0; j<second_directories.length;j++){
               String thirdDirectory = "C:\Users\AG05359\Desktop\bdf-rscz-etl\streamsets\"+directories[i]+"\"+second_directories[j];
               // Andy said why didnt i use some io method that will give me all the directoreis
               String [] third_directories = returnDirectories(thirdDirectory);
               System.out.println("third");
               System.out.println(Arrays.toString(third_directories));
               if(Arrays.asList(third_directories).contains("parm")){
                   System.out.println("true");
                   String [] parm_directories = returnJSONS("C:\Users\AG05359\Desktop\bdf-rscz-etl\streamsets\"+directories[i]+"\"+second_directories[j]+"\parm");
                   System.out.println(Arrays.toString(parm_directories));
                  
                   for(int k =0; k<parm_directories.length;k++){
                      
                       String[] keyValue = returnJsonValues("C:\Users\AG05359\Desktop\bdf-rscz-etl\streamsets\"+directories[i]+"\"+second_directories[j]+"\parm\"+parm_directories[k]);
                       String key = keyValue[0];
                       String value = keyValue[1];
                       if( value.length() <= 4 ){
                          
                      
                      
               sb.append(directories[i]);
               sb.append(',');
               sb.append(second_directories[j]);
               sb.append(',');
               sb.append(parm_directories[k]);
               sb.append(',');
               sb.append(key);
               sb.append(',');
               sb.append(value);
               sb.append(' ');
                       }
                   }
               }
           }
       }
pw.write(sb.toString());
pw.close();
System.out.println("done!");
      
   }
  
   public static String[] returnDirectories(String initDirectory){
       File file = new File(initDirectory);
       String[] directories = file.list(new FilenameFilter() {
       @Override
       public boolean accept(File current, String name) {
       return new File(current, name).isDirectory();
       }
       });
      
       return directories;
   }
  
   public static String[] returnJSONS(String initDirectory){
       File f = new File(initDirectory);
       String[] matchingFiles = f.list(new FilenameFilter() {
       public boolean accept(File dir, String name) {
       return name.endsWith("json");
       }
       });
      
       String[] onlyFirstFile = {matchingFiles[0]};
       return matchingFiles; // return all json files in parm directory
//       return onlyFirstFile; // return only first json file of each parm directory
   }
  
   public static String[] returnJsonValues(String initDirectory) throws FileNotFoundException{
      
       JsonParser parser = new JsonParser();
       System.out.println("below");
       System.out.println(initDirectory);
       String key = "";
       String value = "";
       try{
           Object obj = parser.parse(new FileReader(initDirectory));
           JsonArray jsonArray = (JsonArray) obj;
          
   //       JsonObject jsonObject = jsonArray.get(0).getAsJsonObject().get("key").toString();
       key = jsonArray.get(0).getAsJsonObject().get("key").toString();
       value = jsonArray.get(0).getAsJsonObject().get("value").toString();
       System.out.println(jsonArray.get(0).getAsJsonObject().toString());
           System.out.println(key);
       System.out.println(value);
   } catch (Exception e) {
       key = "iinvalid jsonn";
       value = "iinvalid jsonn";
       System.out.println("invalid json");
       System.out.println("invalid json");
   }
       String [] keyValue = {key.substring(1, key.length()-1),value.substring(1, value.length()-1)};
       System.out.println(keyValue[0]);
       System.out.println(keyValue[1]);      
       return keyValue;
   }
  

}

this should be the output

parm.csv-Excel FILE HOME INSERT PAGE LAYOUTFORMULAS DATA REVIEW VIEW Mohamed, Mazen S Cut E Copy Calibri Wrap Text General Fill Clear Paste Conditional Format as Cell Insert Delete Format Sort &Find& B 1 u .茁·2. EE r Merge & Center. $. % , e Format Painter ormatting TableStyles- Filter Select Font Alignment Cells Editing 4 1 Subject Ni Template File Name Property I Property Value 2 broker ascs 3 broker ascs 4 broker ascs 5 broker ascs 6 broker ascs 7 broker ascs 8 broker ascs 9 broker ascs 10 broker ascs 11 clm 12 clm 13 clm 14clm 15clm 16clm 17 clm 18 clm 19 clm 20clm 21 clm 22clm 23 clm 24 clm BDF BRO BDF SOR BDF BRO BDF SOR BDF BRO BDF SOR BDF BRO BDF SOR BDF BRO BDF SOR BDF BRO BDF SOR BDF BRO BDF SOR BDF BRO BDF SOR BDF BRO BDF SOR BDF CLM BDF SOR BDF CLM BDF SOR BDF CLM BDF SOR BDF CLM BDF SOR BDF CLM BDF SOR BDF CLM BDF SOR BDF CLM BDF SOR BDF CLM BDF SOR BDF CLM BDF SOR BDF CLM BDF SOR BDF CLM BDF SOR BDF CLM BDF SOR BDF CLM BDF SOR BDF CLM BDF SOR 831 831 831 831 831 831 831 831 831 519 囲回凹 100%

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution