2

I have a requirement to parse a CSV file with 1 header record (no Names), one to many detail records and one tail record as follows (input.csv) which needs to be converted into the output.json format.

I have used the below opencsv library to read the records into List of Array of string where each String array in the List represents 1 record.

There is also a bean mapping given in the opencsv but it doesn't have the facility to map the header record into a separate Bean, details records into array of separate beans and tail record into another separate bean.

http://opencsv.sourceforge.net/#reading_without_annotations_column_positions

So I am now stuck to produce the Json from the CSV with a Header, Multiple Detail and One Tail record.

Could someone please share some ideas to achieve this?

input.csv

1,,,

2,DC PIV REL11,D6,

2,DC PIV REL12,ADDED VIA SFTP12,

2,D6,ADDED VIA SFTP6,

3,123,END

output.json

{
    "header": {
    "type": "1",
    "number": "",
    "code": ""
},
{
  "contents": {
    "content": [
      {
        "type": "2",
        "name": "DC PIV REL11",
        "reference": "D6"
      },
      {
        "type": "2",
        "name": "DC PIV REL12",
        "reference": "ADDED VIA SFTP12"
      },
      {
        "type": "2",
        "name": "D6",
        "reference": "ADDED VIA SFTP6"
      }
    ]
},
{ 
"tail": {
    "type": "3",
    "number": "123",
    "code": "END"
 }
}

1 Answer 1

1

I have found the following solution.

I have created 4 DTO classes Root, Header, Contents and Tail with appropriate getter setters and used the below code to solve the problem.

Please Note, I haven't added the validation logic to check whether the record is header, content or tail record in the below code as I assumed the first record is header last but one records are contents and last record is tail.

Hope this helps.

CSVReader reader = new CSVReader(new FileReader(classLoader.getResource("sample_file.txt").getFile()));


    List<String[]> records = reader.readAll();

    int count = 0;
    Header header = new Header();
    Contents contents = new Contents();
    Tail tail = new Tail();
    List<Content> contentList = new ArrayList<Content>();
    for (String[] record : records) {

        if (count == 0 ) {

            header.setRecordType(record[0]);
            header.setNumber(record[1]);
            header.setCode(record[2]);              
        } else if (count < (records.size() - 1)) {

            Content content = new Content();
            content.setRecordType(record[0]);
            content.setName(record[1]);
            content.setReference(record[2]);

            contentList.add(content);
        } else {

            tail.setRecordType(record[0]);
            tail.setNumber(record[1]);
            tail.setCode(record[2]);

        }
        count++;

    }
    contents.setContent(contentList);

    Root root = new Root();
    root.setHeader(header);
    root.setContents(contents);
    root.setTail(tail);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.