1

I am having some problem understanding how to approach this conversion. My csv file has 4 headers: project name, changes, version, team Lead. However I also have the base paths for each projects within the name header like so:

ex:

overallPorjects,null,null,null
overallPorjects/projectBasepath,null,null,null
overallPorjects/projectBasepath/Project1,null,null,null
overallPorjects/projectBasepath/Project1/Blaze,13,12.5,Nix
overallPorjects/projectBasepath/Project1/Sun,13,12.5,Dan
.... similar for other projects (always ordered)
overallPorjects/project2,12,12.5,Nix

the above is a made up output of my csv file, and now I want to make this in to a json object. where the first base path is encapsulating all the projects underneath it with the relevant info. so overallPorjects has all the children that have overallProject in its name, then each children also have children within them until we reach the final project name.

I know this is confusing and I am also very confused, I am not sure how to go about this. If someone can give me some advice or direction on how to approach this that would be very helpful. Thank you.

Anika
  • 103
  • 1
  • 8
  • check if this can help u https://stackoverflow.com/questions/19766266/directly-convert-csv-file-to-json-file-using-the-jackson-library – Pandit Biradar Nov 20 '20 at 10:34

2 Answers2

0
    public static void main(String args[]) throws Exception {
      File input = new File("input.csv");
      try {
         CsvSchema csv = CsvSchema.emptySchema().withHeader();
         CsvMapper csvMapper = new CsvMapper();
         MappingIterator<Map<?, ?>> mappingIterator =  csvMapper.reader().forType(Map.class).with(csv).readValues(input);
         List<Map<?, ?>> list = mappingIterator.readAll();
        System.out.println(list);
      } catch(Exception e) {
         e.printStackTrace();
      }
   }

reference: https://www.tutorialspoint.com/convert-csv-to-json-using-the-jackson-library-in-java

Have a look at this...

Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
0

Im my opinion, you should created

  1. Java class

       public class ProjectData {
            private String projectName; // for example Blaze
            private String fullName; // for example "overallPorjects/projectBasepath/Project1/Blaze"
            private List<ProjectData> childreen = new ArrayList<>();
    
            public String getParent() {
                 int index = fullName.lastIndexOf("/");
                 if(index > 0) {
                     return fullName.substr(0, index);
                 } else {
                     return null; 
                 }
            }
            ...
       }
    
  2. Then created Map<String,ProjectData> with containg values keys like

         "overallPorjects", "overallPorjects/projectBasepath"
    
  3. Then you need iterated for csv (first row is root element), save information to Map, get parent object from map, and add new object to childreen list for this parent

  4. After you just save ProjectData to json,

For example (draft, not checking):

  Map<String,ProjectData> map = new HashMap();
  ProjectData root = null;
  while(isNextInCsv()) {
      ProjectData project = getNextProjectDataFromCSV();
      String parent = project.getParent();
      if(parent == null) {
          root = project;
      } else {
          map.get(parent).getChildreen().add(project); 
      }
      map.put(project.getFullName(), project);
  }
  if(root != null) {
     saveToJson(root);
  }
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59