Search code examples
javajacksonjackson-databind

Getting urecognized field error message when parsing csv


I am trying to convert CSV file to JSON using Jackson library. I have created the POJO class attributes similar to CSV file headers. But even though facing unrecognized field error problem.

Here is my code

    public static List<SalesValidationDetails> parseCSVFile(File file) throws IOException {
                
          CsvMapper csvMapper = new CsvMapper();
          MappingIterator<SalesValidationDetails> eachSale = 
          csvMapper.readerWithTypedSchemaFor(SalesValidationDetails.class).readValues(file);
    return eachSale.readAll();

}

Pojo class:

@JsonPropertyOrder({ "OrderReference", "Date", "Status", "StatusNote", "NewSalePrice", "TransactionPartsNew", "Currency"})
public class SalesValidationDetails {

    private String OrderReference;
    private String Date;
    private String Status;
    private String StatusNote;
    private String NewSalePrice;
    private String TransactionPartsNew;
    private String Currency;
    
    public String getOrderReference() {
    return OrderReference;
}

public String getDate() {
    return Date;
}

public String getStatus() {
    return Status;
}

public String getStatusNote() {
    return StatusNote;
}

public String getNewSalePrice() {
    return NewSalePrice;
}

public String getTransactionPartNew() {
    return TransactionPartsNew;
}

public String getCurrency() {
    return Currency;
}

public void setOrderReference(String OrderReference) {
    OrderReference = OrderReference;
}

public void setDate(String Date) {
    Date = Date;
}

public void setStatus(String Status) {
    Status = Status;
}

public void setStatusNote(String StatusNote) {
    StatusNote = StatusNote;
}

public void setNewSalePrice(String NewSalePrice) {
    NewSalePrice = NewSalePrice;
}

public void setTransactionPartsNew(String TransactionPartsNew) {
    TransactionPartsNew = TransactionPartsNew;
}

public void setCurrency(String Currency) {
    Currency = Currency;
}

@Override
public String toString() {
    return "SalesValidationDetails{" +
            "orderReference='" + OrderReference + '\'' +
            ", date=" + Date +
            ", status='" + Status + '\'' +
            ", statusNote='" + StatusNote + '\'' +
            ", newSalesPrice='" + NewSalePrice + '\'' +
            ", transactionPartsNew='" + TransactionPartsNew + '\'' +
            ", currency='" + Currency + '\'' +
            '}';
}

}

My CSV file looks like this: enter image description here

My error message:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "transactionPartNew" (class com.decathlon.beans.SalesValidationDetails), not marked as ignorable (7 known properties: "currency", "newSalePrice", "statusNote", "orderReference", "date", "status", "transactionPartsNew"])
 at [Source: (com.fasterxml.jackson.dataformat.csv.impl.UTF8Reader); line: 1, column: 94] (through reference chain: com.decathlon.beans.SalesValidationDetails["transactionPartNew"])

Can somebody help me what is wrong here, i have tried all the ways.


Solution

  • Your settlers implemented wrongly, it should be (example for one setter only):

    ...
    
    public void setTransactionPartsNew(String TransactionPartsNew) {
        this.TransactionPartsNew = TransactionPartsNew;
    }
    ...
    

    So, you have to assign value to the object’s field.