I have a Decision path tree which has nodes following a specific pattern i.e. decision -> reason -> decision -> outcome -> terminal. Every decision has a decisionId. Every path ends in a terminal state. The goal is collect all the decisions and aggregate them in a map of decisionId in a DecisionResult object. Every decision object starts with a root. Here is the structure for the types:
export interface Root {
rootId: string
decision: Decision
}
export interface Option {
optionName: string
decision: Decision
}
export interface Decision {
decisionId: string
outcome?: Outcome
options?: Option[]
}
export interface Outcome {
terminal: Reason
}
export interface Reason {
decision: Decision
}
export type DecisionResult = {
rootId: string
rootDecision: Map<string, Decision>,
decisions: Map<string, Decision>,
}
Here is the JSON structure representing the data. I have no idea how to start navigating this structure. I start by getting the keys but don't know how to traverse the tree and add them to a map
{
"rootId": "decision-tree",
"decision": {
"decisionId": "rootOption",
"options": [
{
"optionName": "LynsTheory",
"decision": {
"decisionId": "lynsOption",
"outcome": {
"terminal": "exit"
}
}
},
{
"optionName": "Tensor",
"decision": {
"decisionId": "tensorOption",
"outcome": {
"reason": {
"decision": {
"decisionId": "terminalOption",
"outcome": {
"reason": {
"terminal": "exit"
}
}
}
}
}
}
},
{
"optionName": "Talend",
"decision": {
"decisionId": "optionals",
"options": [
{
"optionName": "Bayes",
"decision": {
"decisionId": "talend",
"options": [
{
"optionName": "cumulative",
"decision": {
"decisionId": "cumulativeI",
"options": [
{
"optionName": "plus",
"decision": {
"decisionId": "plusT",
"outcome": {
"reason": {
"decision": {
"decisionId": "relational",
"outcome": {
"reason": "exit"
}
}
}
}
}
}
]
}
}
]
}
}
]
}
}
]
} }
The resulting structure should look something like this: the keys are on the outside and every child decision is in its parent using the child key:
{
"rootId": "decision-tree",
"rootOption":{
"root": {
"decision": {
"decisionId": "root",
"outcome": {
"terminal": "exit"
}
}
}
},
"lynsOptions":{
"decision":
{
"decisionId": "lynsOptions",
"optionName": "LynsTheory",
"decisions": {
"optionName": "LynsTheory",
"Bayes": {
"decision":{
"outcome":{
"terminal": "exit",
"reason": "Complete"
}
}
},
"tensorOption":{
"optionName": "TensorFlow",
"decision":{
"outcome":{
"terminal": "exit",
"reason": "Complete"
}
}
},
"variableOption":{
"decision":{
"outcome":{
"terminal": "exit",
"reason": "Complete"
}
}
}
}
}
}
}