1

I'm trying to write a code where a csv content is converted to a bean. This is how my code looks like

final String TEMPLATE_FILE="addresses.csv";

    CSVReader template = new CSVReader(new FileReader(TEMPLATE_FILE));
    String [] header = template.readNext();
    ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
    strat.setType(PersonEx.class);
    strat.setColumnMapping(header);
    CsvToBean csv = new CsvToBean();
    List list = csv.parse(strat, template);

In here if a particular attribute of PersonEx.class is missing in the csvHeader that attribute is ascribed as null. Is there a way, I can ignore this and only create a bean of values present in the header?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
R Syed
  • 1,043
  • 5
  • 14
  • 22
  • What is wrong with the new bean containing null values for the attributes not present in the CSV file? Do you want those attributes to have some other default value? – Rob Breidecker Mar 24 '13 at 18:20
  • As I'm converting the bean to a JSON Object, which is my original requirement. I want the JSON object to only contain the CSV header fields and nothing else. I'm thinking once I conver this to a jsonNode using jackson, is there anyway I can remove the elements from jsonNode using there string fields? I've asked the question here. http://stackoverflow.com/questions/15603033/jackson-jsonnode-removing-elements – R Syed Mar 24 '13 at 19:39
  • I would create a bean with only the attributes in your CSV file. – Rob Breidecker Mar 24 '13 at 22:37

0 Answers0