1

I have this json structure from firebase where it's a list of objects but the root of each object is the ID

id root for object, list

How can I serialize this to a list of object with the id/root as a member variable. This is for Spring boot so I would prefer if it were a Jackson2 solution.

This may be the same question as Jackson JSON key as value in Java but my answer is better because it doesn't require an extra/useless class

  • Possible duplicate of [Jackson JSON key as value in Java](https://stackoverflow.com/questions/46984314/jackson-json-key-as-value-in-java) – Vasan Mar 29 '18 at 02:33
  • It is basically the same question but I would think my question is more clear and the answer that I found is better, too. – Matt Goodwin Mar 29 '18 at 03:05

2 Answers2

1
    val reader = ObjectMapper().reader()
    val tree = reader.readTree(testJson)

    val eventList = mutableListOf<Event>()
    tree.fields().iterator().forEach {
        val event = Event(
                it.key,
                it.value.get("name").asText(),
                it.value.get("description").asText(),
                it.value.get("startDate").asText(),
                it.value.get("startTime").asText(),
                it.value.get("endDate").asText(),
                it.value.get("endTime").asText(),
                it.value.get("imageUrl").asText()
        )

        eventList.add(event)
    }

I have found a solution using jackson in kotlin, it's not the prettiest but it works. the fields method returns a map of children. So, I am iterating through the map and adding the key as the object id member and then grabbing the rest of the data from the nested map.

0

Sorry, I am not a pro in Jackson. But there is a simple way to transform your object in JavaScript -- and perhaps this will lay the foundation for something that can be ported to Jackson (java?)

This is the convertData function that does the transform:

var convertData = function (d) {
    var arr = [];
    for(var prop in data) 
    {
        var el = { "id": prop };
        var otherprops = data[prop];
        for(var otherprop in otherprops) 
        {
            el[otherprop] = otherprops[otherprop];
        }
        arr.push(el);
    }
    return arr;
}

Tnis is what my sample data looks like after the conversion (slightly different values than yours):

[{"id":"-L8eoUd5mqJGnXDVSmb0","description":"With a great description","endDate":"12/31/2018","endTime":"03:00","imageUrl":"/favicon.ico","name":"Here's a Good Event","startDate":"12/01/2018","startTime":"12:00"},{"id":"-L8jO6Zhz976hvoLUiga","description":"Another item","endDate":"12/30/2018","endTime":"03:05","imageUrl":"/favicon2.ico","name":"Event #2","startDate":"12/11/2018","startTime":"12:03"}]

Link to JSFiddle: https://jsfiddle.net/2t1s2are/13/

Hope this helps!!

Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73
  • 1
    The json is coming from Firebase, so I can't modify the structure to add the id parent node, but this looks good other wise. I have actually answered my own question using Jackson in Kotlin if you'd like to see it. – Matt Goodwin Mar 29 '18 at 03:04