• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Convert csv to JSON  RSS feed

 
Ranch Hand
Posts: 69
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I am trying to convert data from a csv file to json format.Search on the internet told me that we need to use external libraries.But all the samples that i found had headers included in the CSV file.But I do not have the headers in my CSV file.So I do not know how to parse.
Below is the sample data in CSV :

"JHMZC7840LX200001","356938035643809","8991101200003200001","ATZD6A700","89049032000001000000031233016882","20190322235824123000","Fail","20190323000101123000","Pass"
"JHMZC7840LX200002","356938035643810","8991101200003200002","ATZD6A700","89049032000001000000031233016883","20190322235824123001","Fail","20190323000101123001","Pass"

I need the data in below format :
Data [{
"ColumnA":"JHMZC7840LX200001",
"ColumnB":"356938035643809",
.
.
},
{
"ColumnA":"JHMZC7840LX200002",
"ColumnB":"356938035643810","
.
.
}
]

Here "ColumnA","ColumnB", and so on are some constants(we can say as hardcoded values).
Not sure if any external lib is required for this or we can do this with no String operations.
Here is the code i have so far :

Any help is welcome.
Thanks Udaya
 
Saloon Keeper
Posts: 20657
122
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not need external libraries. Printing JSON can be done without external support methods, though for more complex applications, there are external libraries that can help. For your problem, they're not worth the trouble.

You could use an external library to parse the CSV more reliably. Using a Scanner to split the columns is fine for a file full of numbers and/or simple text, but when you have columns that have quotes and/or commas as part of the column value, that's when you'd want an external library that already knows how to do that - it saves design and coding time to add that extra logic and some other poor slob has already done the debugging.

So you're fine as-is.
 
please buy this thing and then I get a fat cut of the action:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!