1 year ago

#386033

test-img

gstackoverflow

How to create case insensitive CsvSchema ising jackson?

I have the following CSV file:

field_1,field_2
  123, ewfwe
 .....

Lets take a look at the following code:

CsvSchema schema = CsvSchema.emptySchema()
            .withUseHeader(true)
            .withComments()
            .withColumnSeparator(',');
....
MappingIterator<Map<String, String>> iter = csvMapper.readerFor(Map.class)
            .with(schema)
            .readValues(new InputStreamReader(inputStream, replacingDecoder))
...
List<String> requiredFieldsForAdd = Arrays.asList(
            "Field_1",
            "Field_2"
 );           
 CsvSchema schemaWithHeaders = ((CsvSchema) iter.getParserSchema()).withColumnSeparator(columnSeparator);
        for (String field : requiredFieldsForAdd) {
            if (Objects.isNull(schemaWithHeaders.column(field.toLowerCase()))) {
                log.warn("Missing required header for add {}", field);
                return false;
            }
        }
        return true;
}

This code returns false (because of case difference in columns names), but I expect to see true. How can I achieve it?

java

csv

jackson

fasterxml

0 Answers

Your Answer

Accepted video resources