title: Excel to Json converter in JAVA
body: <p>Am trying to covert the excel data into JSON format. And i am successful upto converting excel data by reading the rows and columns and set the data to list and then converting that List to JSON format.</p>
  <p><a href="https://i.stack.imgur.com/wYL3k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wYL3k.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/epo1X.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/epo1X.png" alt="enter image description here" /></a></p>
  <p>But, I stuck in next level of JSON where the format will be like
  <a href="https://i.stack.imgur.com/6B17e.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6B17e.png" alt="enter image description here" /></a></p>
  <p>Here is the code which iam using, But not yet started how to make that nested data like 3 columns data should come in one array like the above image.</p>
  <p><strong>Pojo class</strong>:</p>
  <pre><code>public class Products implements Serializable{
  
  @Id
  @Column(name=&quot;ID&quot;)
  private String id;
  
  @Column(name=&quot;PRODUCTID&quot;)
  private String productId;
  
  @Column(name=&quot;NAMEID&quot;)
  private String nameId;
  
  @Column(name=&quot;NAME&quot;)
  private String name;
  
  @Column(name=&quot;P_DESC&quot;)
  private String desc;
  
  @Column(name=&quot;DIMENSION&quot;)
  private String dimension;
  
  @Column(name=&quot;CATEGORY&quot;)
  private String category;
  
  @Column(name=&quot;SUB_CATEGORY&quot;)
  private String subcategory;
  
  @Column(name=&quot;CATEGORYID&quot;)
  private String categoryId;
  
  @Column(name=&quot;SUBCATEGORYID&quot;)
  private String subcategoryId;
  
  @Column(name=&quot;TAGS&quot;)
  private String tags;
  
  @Column(name=&quot;DESIGNER&quot;)
  private String designer;
  
  @Column(name=&quot;CURR&quot;)
  private String curr;
  
  @Column(name=&quot;POPULARITY&quot;)
  private String popularity;
  
  @Column(name=&quot;RELEVANCE&quot;)
  private String relevance;
  
  @Column(name=&quot;SHORTLISTED&quot;)
  private String shortlisted;
  
  @Column(name=&quot;LIKES&quot;)
  private String likes;
  
  @Column(name=&quot;CREATE_String&quot;)
  private String createDt;
  
  @Column(name=&quot;PAGEID&quot;)
  private String pageId;
  
  @Column(name=&quot;STYLENAME&quot;)
  private String styleName;
  
  @Column(name=&quot;STYLEID&quot;)
  private String styleId;
  
  @Column(name=&quot;PRICERANGE&quot;)
  private String priceRange;
  
  @Column(name=&quot;PRICEID&quot;)
  private String priceId;
  
  @Column(name=&quot;DEFAULT_PRICE&quot;)
  private String defaultPrice;
  
  @Column(name=&quot;DEFAULT_MATERIAL&quot;)
  private String defaultMaterial;
  
  @Column(name=&quot;DEFAULT_FINISH&quot;)
  private String defaultFinish;
  /* setters and getters */
  }
  </code></pre>
  <p><strong>ExceltoJsonConverter class</strong>:</p>
  <pre><code>public static void uploadXLS(MultipartFile file, Document doc)
      throws IOException {
  
  Products products = new Products();
  
  List&lt;Products&gt; productsList = new ArrayList&lt;Products&gt;();
  
  logger.info(&quot;uploadExcel method&quot;);
  HSSFWorkbook wb = null;
  try {
  
       wb= new HSSFWorkbook(file.getInputStream());
          System.out.println(&quot;workbook: &quot;+wb);
          HSSFSheet sheet = wb.getSheetAt(0);
          System.out.println(&quot;worksheet: &quot;+sheet);
          HSSFRow row;
  
          Iterator&lt;Row&gt; iterator = sheet.iterator();
  while (iterator.hasNext()) {
                  products = new Products();
                  Row nextRow = iterator.next();
                  Iterator&lt;Cell&gt; cellIterator = nextRow.cellIterator();
                   Cell cell = cellIterator.next(); 
                      Iterator cells = nextRow.cellIterator();
  
                          cell=(HSSFCell) cells.next();
  
                          if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
                          {
                              System.out.print(cell.getStringCellValue()+&quot; &quot;);
                          }
                          else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
                          {
                              System.out.print(cell.getNumericCellValue()+&quot; &quot;);
                          }
                          else if(HSSFDateUtil.isCellDateFormatted(cell)){
                              Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
                          } else
                          {
                              //U Can Handel Boolean, Formula, Errors
                          }
  
                          products.setId(new DataFormatter().formatCellValue(nextRow.getCell(0)));
                          products.setProductId(new DataFormatter().formatCellValue(nextRow.getCell(1)));
                          products.setNameId(new DataFormatter().formatCellValue(nextRow.getCell(2)));
                          products.setName(new DataFormatter().formatCellValue(nextRow.getCell(3)));
                          products.setDesc(new DataFormatter().formatCellValue(nextRow.getCell(4)));
                          products.setDimension(new DataFormatter().formatCellValue(nextRow.getCell(5)));
                          products.setCategory(new DataFormatter().formatCellValue(nextRow.getCell(6)));
                          products.setSubcategory((new DataFormatter().formatCellValue(nextRow.getCell(7))));
                          products.setCategoryId(new DataFormatter().formatCellValue(nextRow.getCell(8)));
                          products.setSubcategoryId((new DataFormatter().formatCellValue(nextRow.getCell(9))));
                          products.setTags((new DataFormatter().formatCellValue(nextRow.getCell(10))));
                          products.setDesigner((new DataFormatter().formatCellValue(nextRow.getCell(11))));
                          products.setCurr((new DataFormatter().formatCellValue(nextRow.getCell(12))));
                          products.setPopularity((new DataFormatter().formatCellValue(nextRow.getCell(13))));
                          products.setRelevance((new DataFormatter().formatCellValue(nextRow.getCell(14))));
                          products.setShortlisted((new DataFormatter().formatCellValue(nextRow.getCell(15))));
                          products.setLikes((new DataFormatter().formatCellValue(nextRow.getCell(16))));
                          products.setCreateDt((new DataFormatter().formatCellValue(nextRow.getCell(17))));
                          products.setPageId((new DataFormatter().formatCellValue(nextRow.getCell(18))));
                          products.setStyleName((new DataFormatter().formatCellValue(nextRow.getCell(19))));
                          products.setStyleId((new DataFormatter().formatCellValue(nextRow.getCell(20))));
                          products.setPriceRange((new DataFormatter().formatCellValue(nextRow.getCell(21))));
                          products.setPriceId((new DataFormatter().formatCellValue(nextRow.getCell(22))));
                          products.setDefaultPrice((new DataFormatter().formatCellValue(nextRow.getCell(23))));
                          products.setDefaultMaterial((new DataFormatter().formatCellValue(nextRow.getCell(24))));
                          products.setDefaultFinish((new DataFormatter().formatCellValue(nextRow.getCell(25))));
  
                          Map&lt;String, String&gt; inputMap = new HashMap&lt;String, String&gt;();
                          inputMap.put(&quot;name&quot;, &quot;Java2Novice&quot;);
                          inputMap.put(&quot;site&quot;, &quot;http://java2novice.com&quot;);
                          productsList.add(products);
                      System.out.println(productsList);
                  
  
                       
             // JSON CONVERTER
      ObjectMapper mapper = new ObjectMapper();
  
      System.out.println(&quot;productsList: &quot;+products);
      DateFormat dateFormat = new SimpleDateFormat(&quot;yyyyMMddHHmmss&quot;);
        Date date = new Date();
        String location = dateFormat.format(date);
        System.out.println(&quot;productsList final: &quot;+products);
  
         // Convert object to JSON string and save into file directly
         mapper.writeValue(new File(&quot;D:\\&quot;+location+&quot;mygubbi.json&quot;), productsList);
          // Convert object to JSON string and save into file directly
          mapper.writeValue(new File(&quot;D:\\products.json&quot;), productsList);
          
          // Convert object to JSON string
          String jsonInString = mapper.writeValueAsString(productsList);
          System.out.println(&quot;JsonInString &quot; +jsonInString);
  
          // Convert object to JSON string and pretty print
          jsonInString = mapper.writerWithDefaultPrettyPrinter()
                  .writeValueAsString(products);
          System.out.println(&quot;Final Json&quot; +mapper.writerWithDefaultPrettyPrinter()
                  .writeValueAsString(products));
          mapper.writeValue(new File(&quot;D:\\productsJson.json&quot;), jsonInString);
  
  
          }
          } catch (JsonGenerationException e) {
      e.printStackTrace();
  } catch (JsonMappingException e) {
      e.printStackTrace();
  } catch (IOException e) {
      e.printStackTrace();
  }
  finally {
  
  
  }
  }
  </code></pre>
  <p>Please suggest me how to approach to get multiple columns data in Json format as shown below
  <a href="https://i.stack.imgur.com/z03H8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/z03H8.png" alt="enter image description here" /></a></p>
  <p>A BIG Thank you in advance... Hoping anyone can make my day today :)</p>