I want to convert an excel table (or CSV table) to a JSON object in the form of Key-Value pair. I cannot use any online service as files are really large.
For example, my table looks something like this:
| name | place | animal |
|---|---|---|
| Himanshu | India | Cat |
| Kamal | London | Dog |
| John | Turkey | Lion |
And I want my response similar to this:
{
{"name":"Himanshu",
"place":"India",
"animal":"Cat"
},
{
"name":"Kamal",
"place":"London",
"animal":"Dog"
},
{
"name":"John",
"place":"Turkey",
"animal":"Lion"
}
}
As I have to post this as a response, I tried to convert it to a list of arrays using the following code :
public static List<String[]> get(String file) {
String delimiter = ",";
String line;
List lines = new ArrayList<>();
try (Scanner s = new Scanner(new File(file))) {
while (s.hasNext()) {
line = s.next();
List values = Arrays.asList(line.split(delimiter));
lines.add(values);
}
} catch (Exception e) {
System.out.println(e);
}
return lines;
}
Now the issue with such a code is that not only it doesn't return output as a list of arrays but also it isn't very generalized and cannot handle any real-life exceptions. Looping through an excel file with hundreds of thousands of entries is using such code would require a lot of time and resources. And we have to work with many such files.
The language I am using is JAVA (Springboot as a framework). It would be great if you could suggest an efficient library to handle such cases. Suggestions for libraries in other languages are welcome but the priority is to do this using JAVA.
Thank you.