Generating Reports
  1. Knowledge Base
  2. Generating Reports

Reports - CSV Parsing Using Column Names

Important Instruction for All Clients

When submitting data to KOR reports 

Clients must always reference fields by their column header names, not by the column ordering.

KOR periodically enhances its report templates. These updates may involve adding, removing, or reordering fields. Column order is not fixed and may change without prior notice. However, header names will always remain stable and are the authoritative reference point.

Failure to write data to headers may result in misaligned submissions, rejected files, or inaccurate reporting.


This guide demonstrates how you can write client-side CSV parsing code using column names instead of column indexes. This approach ensures your code remains robust even when the column order changes. The examples are in Java, but this can be done in any programming language.

 
Parsing CSV by column names in Java using a library

We'll use OpenCSV, a popular library for CSV parsing. You can also do this manually with BufferedReader, but OpenCSV simplifies handling edge cases like quotes and commas inside fields.

 
1. Add OpenCSV Dependency

If you’re using Maven, add this to your pom.xml:

XML
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.8</version> <!-- Use latest -->
</dependency>
 
2. Sample CSV File (people.csv)
id,first_name,last_name,email
1,John,Doe,john.doe@example.com
2,Jane,Smith,jane.smith@example.com
 
3. Code to Parse CSV by Header Name
Java
import com.opencsv.CSVReaderHeaderAware;
import java.io.FileReader;
import java.util.Map;

public class CsvParserByHeader {
public static void main(String[] args) {
String filePath = "people.csv";

try (CSVReaderHeaderAware reader = new CSVReaderHeaderAware(new FileReader(filePath))) {
Map<String, String> row;
while ((row = reader.readMap()) != null) {
String id = row.get("id");
String firstName = row.get("first_name");
String lastName = row.get("last_name");
String email = row.get("email");

System.out.printf("User: %s %s (%s), ID: %s%n", firstName, lastName, email, id);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
  • CSVReaderHeaderAware maps each row to a Map<String, String>, with keys from the header row.
  • You access data by column name, not position.
  • It automatically skips the header line and keeps track of it.
 
Advantages
  • Your code doesn't break if the column order changes.
  • It’s readable and maintainable.
  • Safe for large CSVs if processed line-by-line.
 
Alternative without an external library
Java
import java.io.*;
import java.util.*;

public class CsvParserManual {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("people.csv"));
String[] headers = reader.readLine().split(",");

Map<String, Integer> headerIndex = new HashMap<>();
for (int i = 0; i < headers.length; i++) {
headerIndex.put(headers[i].trim(), i);
}

String line;
while ((line = reader.readLine()) != null) {
String[] values = line.split(",");

String id = values[headerIndex.get("id")];
String firstName = values[headerIndex.get("first_name")];
String lastName = values[headerIndex.get("last_name")];
String email = values[headerIndex.get("email")];

System.out.printf("User: %s %s (%s), ID: %s%n", firstName, lastName, email, id);
}

reader.close();
}
}