Struggling to figure out how to create nested JSON based of a HashMap. Basically I have a List of HashMap, that has non unique key/value pairs. And I need to turn this List of HashMap into a unique JSON Object.
Here is an example of what I'm trying to describe: List of HashMap looks like:
[
{PERIOD: 201801, ID: 12345},
{PERIOD: 201801, ID: 12346},
{PERIOD: 201801, ID: 12347},
{PERIOD: 201802, ID: 12345},
{PERIOD: 201802, ID: 12347},
{PERIOD: 201803, ID: 12345},
]
And I'd like my result to be:
{
"NAME": "Results",
"DETAILS": [
{
"PERIOD": 201801,
"DETAILS": [
{ "ID": 12345 },
{ "ID": 12346 },
{ "ID": 12347 }
},
{
"PERIOD": 201802,
"DETAILS": [
{ "ID": 12345 },
{ "ID": 12347 }
},
{
"PERIOD": 201803,
"DETAILS": [
{ "ID": 12345 }
}
]
}
One caveat is that they key/value pair in the HashMap can be any number of items, not just two like I've described in the example above. I've tried doing a recursive function, but I keep hitting a wall.
Edit: This is not a duplicate of this question. I know how to serialize a HashMap into a JsonObject using these methods. I've done it several times. I'm asking how to logically analyze data that is in a HashMap and create a JsonObject based of that logic.
Edit 2: Example of multiple levels:
[
{PERIOD: 201801, ID: 12345, MANAGER: "Dave"},
{PERIOD: 201801, ID: 12345, MANAGER: "Jill"},
{PERIOD: 201801, ID: 12346, MANAGER: "Dave"},
{PERIOD: 201801, ID: 12347, MANAGER: "Jon"},
{PERIOD: 201802, ID: 12345, MANAGER: "Rob"},
{PERIOD: 201802, ID: 12347, MANAGER: "Dave"},
{PERIOD: 201803, ID: 12345, MANAGER: "Bailey"},
]
And here is the JSON:
{
"NAME": "Results",
"DETAILS": [
{
"PERIOD": 201801,
"DETAILS": [
{
"ID": 12345,
"DETAILS": [
"MANAGER": "Dave",
"MANAGER": "Jill"
]
},
{
"ID": 12346,
"DETAILS": [
"MANAGER": "Dave"
]
},
{
"ID": 12347,
"DETAILS": [
"MANAGER": "Jon"
]
}
},
{
"PERIOD": 201802,
"DETAILS": [
{
"ID": 12345,
"DETAILS": [
"MANAGER": "Rob"
]
},
{
"ID": 12347,
"DETAILS": [
"MANAGER": "Dave"
]
}
},
{
"PERIOD": 201803,
"DETAILS": [
{
"ID": 12345,
"DETAILS": [
"MANAGER": "Bailey"
]
}
}
]
}