โ11-16-2022 06:17 AM
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.
โ11-16-2022 07:07 AM
Assuming your input is a single document with an array of objects, rather than a stream of individual documents.
With that assumption you can use a mapper with the expression:
jsonPath($, "$[*].tree")
and target path: tree
This will collect all tree objects from every element in the array, then write that list to the target path tree