0

How to read same more than one column having same name in Java

Description: I am converting CSV to JSON by reading csv schema and successful into it, but the problem is that the CSV file contains two column having same name, and it is overriding the second column value into first one and not showing the second column in json.

My requirement is to read both column values into json and pass on, as The code block is below, I browsed to find a work around, yet not successful! My Application is written in java.

Code

public List<Map<?, ?>>  convert(String file) throws Exception {
       File input = new File(file);
       try {

          CsvSchema csv = CsvSchema.emptySchema().withHeader(); 
          CsvMapper csvMapper = new CsvMapper(); 
          MappingIterator<Map<?, ?>> mappingIterator =  csvMapper.reader().forType(Map.class).with(csv).readValues(input); 
          List<Map<?, ?>> list = mappingIterator.readAll(); 
          return list; 
      } catch(Exception e) { 
         e.printStackTrace(); 
         return null; 
      } 
   } 

enter image description here

JSON I want

[
    {
        UserName=DasKhatri, Pass=777, Name=Guru, FamilyName=Khatri, ShortName=GK, UserName=GuruKhatri, Pass=111
    }
]

JSON I am Getting

[
    {
        UserName=GuruKhatri, Pass=111, Name=Guru, FamilyName=Khatri, ShortName=GK
    }
]
Das Khatri
  • 93
  • 6
  • 3
    "the problem is that the CSV file contains two column having same name, and it is overriding the second column value into first one and not showing the second column in json." Well, yes: a JSON object is not allowed to have duplicate keys - that's part of the format specification. Why exactly does your CSV look like this? How am I supposed to interpret a line like the one shown - how does the same person have multiple usernames and passwords? – Karl Knechtel Jul 28 '20 at 13:46
  • 1
    Can you write out by hand how you want the JSON to look for this example? – Karl Knechtel Jul 28 '20 at 13:47
  • [ { UserName=DasKhatri, Pass=777, Name=Guru, FamilyName=Khatri, ShortName=GK, UserName=GuruKhatri, Pass=111 } ] – Das Khatri Jul 28 '20 at 14:10
  • That’s impossible. You’ll need to instead have an array of objects for each row, and have a name property for each one, or pick a “primary key” and have multi-values fields, etc. Or fix the data source. – Dave Newton Jul 28 '20 at 14:21
  • @KarlKnechtel This is requirement of application, 1- First UserName is creating a record, 2- Second UserName is going to Authorize that record. And this is one flow, Application will read both UserName in same flow. And login screen is the same! So I am supposed to use same column name – Das Khatri Jul 28 '20 at 14:22
  • 1
    The json format can't have two properties with the same name. The creator and authorizer are completely different pieces of information in your use case. You may be supposed to show the text "UserName" on two different screens but you need to keep track of which is which in your code. – JollyJoker Jul 28 '20 at 14:32
  • 1
    Be more verbose in your column descriptions – Mark Jeronimus Jul 28 '20 at 15:18
  • I am getting csv data into a List> , so regarding data structure is there any other work around where It can be possible ? – Das Khatri Jul 28 '20 at 15:20

0 Answers0