R Syed
R Syed

Reputation: 1093

Converting csv row to JSON object in Java

I've a csv file something similar to this

"name.firstName","name.givenName","name.DisplayName","phone.type","phone.value"
"john","maverick","John Maverick","mobile","123-123-123"
"jim","lasher","Jim Lasher","mobile","123-123-123"

I want to convert the 2nd and 3rd row into JSON objects.Using the first row as Header. So the result will be

[
{  
"name": {
    "firstName": "john",
    "givenName": "maverick",
    "DisplayName": "John Maverick"
},
"phone": {
    "type": "mobile",
    "value": "123-123-123"
}
},
{
"name": {
    "firstName": "john",
    "givenName": "maverick",
    "DisplayName": "John Maverick"
},
"phone": {
    "type": "mobile",
    "value": "123-123-123"
}
]

Any idea how to achieve this?

Upvotes: 4

Views: 10544

Answers (1)

Rob Breidecker
Rob Breidecker

Reputation: 604

Here is a Java library that may help you. http://www.jonathanhfisher.co.uk/playpen/csv2json/index.htm

Here is a JavaScript library that may or may not be useful to you. http://www.cparker15.com/code/utilities/csv-to-json/

And finally, here is a past answer that may be useful. I like the OpenCSV solution. However, instead of JAXB, you could use Jackson. Converting an CSV file to a JSON object in Java

Upvotes: 2

Related Questions