I’ve a Java object structure corresponding to following JSON:
{
"parentClass": {
"parentField1": "parentFieldValue1",
"parentField2": "parentFieldValue2",
"childClass": {
"childField1": "childFieldValue1",
"childField2": "childFieldValue2"
},
"childClassList": [
{
"stringField": "stringField1",
"integerField": "integerField1"
},
{
"stringField": "stringField2",
"integerField": "integerField2"
}
]
}
}
I want to flatten it to following structure:
{
"parentClass.parentField1": "parentFieldValue1",
"parentClass.parentField2": "parentFieldValue2",
"parentClass.childClass.childField1": "childFieldValue1",
"parentClass.childClass.childField2": "childFieldValue2",
"parentClass.childClassList[0].stringField": "stringField1",
"parentClass.childClassList[0].integerField": "integerField1",
"parentClass.childClassList[1].stringField": "stringField2",
"parentClass.childClassList[1].integerField": "integerField2"
}
Is there a way to do it?
2
Answers
I've found a way using following code:
Implementation:
Library Josson has function
flatten()
to do the transformation.https://github.com/octomix/josson
Output