Forum Discussion

philliperamos's avatar
philliperamos
Contributor
3 years ago

JSON Formatting and Merging

Hello All,
I have encountered a scenario where we need to map all the element with key, “tree”, to a single array of the same name. I have provided an example below
Input:

[{
“tree”: {
“name”: “DB1”
“type”: “database”
“children”: [{
“name”: “SCHA”
“type”: “schema”
“children”: [{
“name”: “TABLEA”
“type”: “table”
“leaves”: [{
“name”: “COA”,
“type”: “column”
}, {
“name”: “COB”,
“type”: “column”
}, {
“name”: “COC”,
“type”: “column”
}]
}]
}]
},
{
“tree”: {
“name”: “DB2”
“type”: “database”
“children”: [{
“name”: “SCHB”
“type”: “schema”
“children”: [{
“name”: “TABLEB”
“type”: “table”
“leaves”: [{
“name”: “CO1”,
“type”: “column”
}, {
“name”: “CO2”,
“type”: “column”
}, {
“name”: “CO3”,
“type”: “column”
}]
}]
}]
}
}
}]

Intended output:

{
“tree”: [{
“name”: “DB1”
“type”: “database”
“children”: [{
“name”: “SCHA”
“type”: “schema”
“children”: [{
“name”: “TABLE1”
“type”: “table”
“leaves”: [{
“name”: “CO1”,
“type”: “column”
}, {
“name”: “CO2”,
“type”: “column”
}, {
“name”: “CO3”,
“type”: “column”
}]
}]
}]
}, {

    "name": "DB2"
    "type": "database"
    "children": [{
        "name": "SCHB"
        "type": "schema"
        "children": [{
            "name": "TABLE1"
            "type": "table"
            "leaves": [{
                "name": "CO1",
                "type": "column"
            }, {
                "name": "CO2",
                "type": "column"
            }, {
                "name": "CO3",
                "type": "column"
            }]
        }]
    }]
}]

}

Essentially, instead of having individual objects with the name tree we are trying to create a single tree array with all the data inside. Any suggestions on this would be really helpful.
Thank you.