I have the following unnormalized csv file
user_id,nickname,joinDate,product_id,price
1,kmh,2023-07-24,P131,3000
1,kmh,2023-07-24,P132,4000
1,kmh,2023-07-24,P133,7000
1,kmh,2023-07-24,P134,9000
2,john,2023-07-24,P135,2500
2,john,2023-07-24,P136,6000
3,alice,2023-07-25,P137,4500
3,alice,2023-07-25,P138,8000
I’m going to change this to the following json format (or java object).
[
{
"user_id": 1,
"nickname": "kmh",
"joinDate": "2023-07-24",
"orders": [
{
"product_id": "P131",
"price": 3000
},
{
"product_id": "P132",
"price": 4000
},
{
"product_id": "P133",
"price": 7000
},
{
"product_id": "P134",
"price": 9000
}
]
},
{
"user_id": 2,
"nickname": "john",
"joinDate": "2023-07-24",
"orders": [
{
"product_id": "P135",
"price": 2500
},
{
"product_id": "P136",
"price": 6000
}
]
},
{
"user_id": 3,
"nickname": "alice",
"joinDate": "2023-07-25",
"orders": [
{
"product_id": "P137",
"price": 4500
},
{
"product_id": "P138",
"price": 8000
}
]
}
]
I’ve been searching for quite a long time and haven’t found a library or tool that enables this .
I have so many different types of csv that I need tools or libraries to change all of these. Are there any libraries or tools that make this possible?
2
Answers
All you need is a way to parse the CSV to a Java object. You can do this manually, or by using an existing library.
For example, you can use Jackson with the CSV data format like this:
Which prints:
For your case, you can deserialize the csv into JsonNode directly without creating POJO class. And then use JSON library Josson to transform the JSON by function
group()
.Function
group()
Functoin
map()
Output