Reputation: 140
This is the JSON format which I am getting through the API.
Currently generated JSON format
[
{
"question":"q1",
"option":"a"
},
{
"question":"q1",
"option":"b"
},
{
"question":"q1",
"option":"c"
},
{
"question":"q1",
"option":"d"
},
{
"question":"q2",
"option":"a"
},
{
"question":"q2",
"option":"b"
}
]
After receiving and converting the above json formar, I want to send the below json format. but I am facing issue to creating below mentioned json format. suppose the list contains above JSON format then what I have tried is:
List finalList = new ArrayList();
list.stream.forEach(k - > {
List optionList = new ArrayList();
Map m = new HashMap();
if (!m.containsKey(k.getQuestion)) {
m.put("ques", k.getQuestion());
}
optionList.add(k.getOption());
m.put("option", optionList);
finalList.add(m);
})
System.out.println(finalList);
but above code is not returning specific prefered JSON format.
JSON format which I want to generate
[
{
"question":"q1",
"option":[
"a",
"b",
"c",
"d"
]
},
{
"question":"q2",
"option":[
"a",
"b"
]
}
]
Upvotes: 1
Views: 79
Reputation: 4088
class SourceData {
private String question;
private String option;
// getters and setters
}
class TargetData {
private String question;
private List<String> options;
// getters and setters
}
List<SourceData> sourceDatas = new ObjectMapper().readValue(jsonString, new TypeReference<List<SourceData>>() {});
List<TargetData> targetDatas = new ArrayList<>();
sourceDatas.stream().collect(Collectors.groupingBy(item -> item.getQuestion()))
.forEach((question, itemList) -> {
TargetData targetData = new TargetData();
targetData.setQuestion(question);
List<String> options = itemList.stream().map(SourceData::getOption).collect(Collectors.toList());
targetData.setOptions(options);
targetDatas.add(targetData);
});
Upvotes: 1