Menu

#123 how to transform data when the column header in csv file is not the same as the pojo

v1.0 (example)
closed
nobody
None
5
2023-11-22
2023-11-18
No

Team, thank you for building such an awesome library, have a basic question regarding the CsvToBeanBuilder, Have tried to google, unfortunately havent found such requirement.

Here are my csv Header,
Id, Name , Department, JobTitle

I have my pojo created as

@Getter
@Setter
@ToString
public class Democsv 
{

    private String Id;
    private String Name;
    private String Department;
    private String JobTitle;
}

my requirement is, not every time the customer will fill the csv file with all data, sometimes they will send only two columns ex Id, Department, sometimes three fields Id, Name, Jobtitle.

In this case how to build a bean model based on the csv file ? Or in general based on the input csv file how to use the CsvToBeanBuilder ?

Many thanks in advance.
Ronald

Discussion

  • Ronald Selvanathan

    am using version 5.8 , using gradle build

     

    Last edit: Ronald Selvanathan 2023-11-18
  • Scott Conway

    Scott Conway - 2023-11-18

    I am not sure if I fully understand the question. If it is just for the basic documentation take a look at the quickstart at https://opencsv.sourceforge.net/#quick_start

    If I remember correctly by default required is false so it is okay if a header is missing - the value will just be default (null, 0, false, etc)

     
  • Ronald Selvanathan

    @sconway thank you , what i have intended to ask was, is the Bean Builder is a strict data model, meaning, the POJO should exactly match to the CSV column header ? e.g If the POJO is of four fields like the above, the CSV file should contain all four columns for bean building , if the CSV file contains only two columns, will still the bean be processed ? any help , much appreciated !!

     

    Last edit: Ronald Selvanathan 2023-11-18
  • Scott Conway

    Scott Conway - 2023-11-18

    K - so no there is no requirement that the value matches the name, nothing is going to blow up - the worst that will happen is that the beans will not populate.

    Under the covers opencsv takes the headers and looks for getters setters for each of them so for Id it will look for a getId and setId. As long as those are there you should have no problem. My only question, which is one admittedly I have not used much, is that you are auto generating your getters and setters but I don't think that will be an issue.

    But your csv file could have 100 columns in it and your bean only have four and it will work no problem - opencsv will fill in what it can find a match for. The same goes in reverse too. The only time you will have problems is if you use one of the binding annotations and put in that a particular field in a bean is required, if the column is not present you will have exceptions thrown.

    Speaking of annotations look at https://opencsv.sourceforge.net/#annotations_2 At the CsvBindByName annotation which will show you what to do if you have odd names in the headers.

     
  • Ronald Selvanathan

    Perfect !! Thank you @sconway Scott !! , this is of great help. This will solve the main hurdle in my software, because based on the data from the CSV, the REST API will be invoked. If my bean is able to populate the input csv , the REST calls will be succeeded. Thank you for the clarification.

    Have a nice weekend !
    Cheers !!

     
  • Scott Conway

    Scott Conway - 2023-11-22
    • status: open --> closed
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.