1

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"
                    ]
                }
        }
    ] 
}
tyejae
  • 47
  • 1
  • 9
  • 2
    Possible duplicate of [How to convert hashmap to JSON object in Java](https://stackoverflow.com/questions/12155800/how-to-convert-hashmap-to-json-object-in-java) – Aleks G Dec 10 '18 at 14:22
  • I voted to close this question as a duplicate of https://stackoverflow.com/questions/12155800/how-to-convert-hashmap-to-json-object-in-java - specifically, check this answer - https://stackoverflow.com/a/12156646/717214 – Aleks G Dec 10 '18 at 14:22
  • 2
    It is not a duplicate of those other questions. Those are specifically asking how to convert a HashMap into JSON. That would be nice, but isn't applicable as I need to evaluate the values in the HashMap and turn those keys into a JSON Object themselves. – tyejae Dec 10 '18 at 14:39
  • Can you give an example illustrating `key/value pair in the HashMap can be any number of items`? – A4L Dec 10 '18 at 14:41
  • @A4L I made an edit to illustrate adding another level to the key/value pair. – tyejae Dec 10 '18 at 14:51

1 Answers1

0

Solved this issue:

private void insertValueIntoDetails(String key, String value, JsonArray details,  int targetIndex, int currentIndex) {
    // if the targetIndex equals the currentIndex ?
    if (targetIndex == currentIndex) {
        // if details doesn't have the value, then insert it
        AtomicBoolean hasValue = new AtomicBoolean(false);
        details.forEach(item -> {
            if (((JsonObject)item).get("ID").getAsString().equals(value.toString())) {
                hasValue.set(true);
            }
        });
        if (!hasValue.get()) {
            JsonObject o = new JsonObject();
            o.addProperty("ID", value);
            o.add("Details", new JsonArray());
            details.add(o);
        }
    }
    // if the targetIndex > currentIndex
    if (targetIndex > currentIndex) {
        // call insertValueIntoDetails with key, value, details of the currentIndex, targetIndex, currentIndex + 1
        JsonArray deets = getDetails(key, details, currentIndex);
        insertValueIntoDetails(key, value, deets, targetIndex, currentIndex + 1);
    }
}
tyejae
  • 47
  • 1
  • 9