I have 2 queries that return the same object for earliest and latest datasets.
eg :
earliestDataset = [
{'id' : 123, 'highlights': 2, 'created_date': 2024-01-01, 'saves':5, 'wins':3},
{'id' : 124, 'highlights': 3, 'created_date': 2024-01-03, 'saves':2, 'wins':1},
{'id' : 253, 'highlights': 5, 'created_date': 2024-02-02, 'saves':3, 'wins':7}]
latestDataset = [
{'id' : 123, 'highlights': 6, 'created_date': 2024-04-01, 'saves':22, 'wins':14},
{'id' : 124, 'highlights': 9, 'created_date': 2024-04-03, 'saves':8, 'wins':9},
{'id' : 253, 'highlights': 12, 'created_date': 2024-03-02, 'saves':15, 'wins':20}]
I wanted to merge the datasets and return in new json format such as :
{
"id": "123",
"highlights": [
{
"earliest": "2",
"latest": "6",
"difference": "+4"
}
],
"created_date": [
{
"earliest": "2024-01-01",
"latest": "2024-04-01"
}
],
"saves": [
{
"earliest": "5",
"latest": "22",
"difference": "+17"
}
],
"wins": [
{
"earliest": "7",
"latest": "20",
"difference": "+13"
}
]
}
I don’t know if I should use map or stream to compare both list, find id and map the values into new Json object. Any help much appreciated, thanks in advance!
2
Answers
I think the best choice is to use
Map
withJackson
.Output:
You may try JSON library Josson to do transformation.
https://github.com/octomix/josson
Output