skip to Main Content

Any suggestions on how to update below JSON object as expected one in Javascript?

Have been trying to flatten JSON object based on JSON key,

  • ..outer_component[x] contains section{} then moved to outer_component[x].outer_body.outer_component[x]
  • ..outer_component[x] contains outer_body{} then keep on iterate till it reaches section{} and move outer_component[x].outer_body.outer_component[x]

Received JSON Object:

{
  "document": {
    "outer_component": [
      {
        "outer_body": {
          "outer_component": [
            {
              "outer_body": {
                "outer_component": [
                  {
                    "section": {
                      "id": [
                        {
                          "root": "INNER_11"
                        }
                      ]
                    }
                  },
                  {
                    "section": {
                      "id": [
                        {
                          "root": "INNER_12"
                        }
                      ]
                    }
                  }
                ]
              }
            },
            {
              "outer_body": {
                "outer_component": [
                  {
                    "section": {
                      "id": [
                        {
                          "root": "INNER_21"
                        }
                      ]
                    }
                  },
                  {
                    "section": {
                      "id": [
                        {
                          "root": "INNER_22"
                        }
                      ]
                    }
                  }
                ]
              }
            }
          ]
        }
      },
      {
        "outer_body": {
          "outer_component": [
            {
              "outer_body": {
                "outer_component": [
                  {
                    "outer_body": {
                      "outer_component": [
                        {
                          "outer_body": {
                            "outer_component": [
                              {
                                "section": {
                                  "id": [
                                    {
                                      "root": "INNER_31"
                                    }
                                  ]
                                }
                              },
                              {
                                "section": {
                                  "id": [
                                    {
                                      "root": "INNER_32"
                                    }
                                  ]
                                }
                              }
                            ]
                          }
                        },
                        {
                          "outer_body": {
                            "outer_component": [
                              {
                                "section": {
                                  "id": [
                                    {
                                      "root": "INNER_33"
                                    }
                                  ]
                                }
                              },
                              {
                                "section": {
                                  "id": [
                                    {
                                      "root": "INNER_34"
                                    }
                                  ]
                                }
                              }
                            ]
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    ]
  }
}

Expected Output:

{
    "document": {
        "outer_component": [{
            "section": {
                "id": [{
                    "root": "INNER_11"
                }]
            }
        }, {
            "section": {
                "id": [{
                    "root": "INNER_12"
                }]
            }
        }, {
            "section": {
                "id": [{
                    "root": "INNER_21"
                }]
            }
        }, {
            "section": {
                "id": [{
                    "root": "INNER_22"
                }]
            }
        }, {
            "section": {
                "id": [{
                    "root": "INNER_31"
                }]
            }
        }, {
            "section": {
                "id": [{
                    "root": "INNER_32"
                }]
            }
        }, {
            "section": {
                "id": [{
                    "root": "INNER_33"
                }]
            }
        }, {
            "section": {
                "id": [{
                    "root": "INNER_34"
                }]
            }
        }]
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    var doc_obj = {
      "document": {
        "outer_component": [
          {
            "outer_body": {
              "outer_component": [
                {
                  "outer_body": {
                    "outer_component": [
                      {
                        "section": {
                          "id": [
                            {
                              "root": "INNER_11"
                            }
                          ]
                        }
                      },
                      {
                        "section": {
                          "id": [
                            {
                              "root": "INNER_12"
                            }
                          ]
                        }
                      }
                    ]
                  }
                },
                {
                  "outer_body": {
                    "outer_component": [
                      {
                        "section": {
                          "id": [
                            {
                              "root": "INNER_21"
                            }
                          ]
                        }
                      },
                      {
                        "section": {
                          "id": [
                            {
                              "root": "INNER_22"
                            }
                          ]
                        }
                      }
                    ]
                  }
                }
              ]
            }
          },
          {
            "outer_body": {
              "outer_component": [
                {
                  "outer_body": {
                    "outer_component": [
                      {
                        "outer_body": {
                          "outer_component": [
                            {
                              "outer_body": {
                                "outer_component": [
                                  {
                                    "section": {
                                      "id": [
                                        {
                                          "root": "INNER_31"
                                        }
                                      ]
                                    }
                                  },
                                  {
                                    "section": {
                                      "id": [
                                        {
                                          "root": "INNER_32"
                                        }
                                      ]
                                    }
                                  }
                                ]
                              }
                            },
                            {
                              "outer_body": {
                                "outer_component": [
                                  {
                                    "section": {
                                      "id": [
                                        {
                                          "root": "INNER_33"
                                        }
                                      ]
                                    }
                                  },
                                  {
                                    "section": {
                                      "id": [
                                        {
                                          "root": "INNER_34"
                                        }
                                      ]
                                    }
                                  }
                                ]
                              }
                            }
                          ]
                        }
                      }
                    ]
                  }
                }
              ]
            }
          }
        ]
      }
    }
    
    //Dynamic Components
    var dyn_component = dynamic_document(
        doc_obj,
        "section", 
        []
    )
    
    //Assigning constructed Dynamic Components
    doc_obj["document"]["dyn_component"] = dyn_component
    
    //Delete Outer Component
    delete doc_obj["document"]["outer_component"]
    
    console.log(JSON.stringify(doc_obj))
    
    /*
    Function: To dynamically fetch component's section
    */
    function dynamic_document(
        doc, req_component, sections
    ) {
        if (doc != null) {
            if (Array.isArray(doc)) {
                doc.forEach((arrayItem) => {
                    dynamic_document(arrayItem, req_component, sections);
                });
            } else if (typeof doc == 'object') {
                Object.keys(doc).forEach((key) => {
                    if (key == req_component) {
                        sections.push(doc);
                    } else {
                        dynamic_document(doc[key], req_component, sections);
                    }
                });
            }
        }
    
        return sections;
    }


  2. I totally agree with other users comments but you can also give a try to below code,

      const result = {
      document: {
        outer_component: inputJSON.document.outer_component.map((outerComponent) => {
          return {
            outer_body: {
              outer_component: outerComponent.outer_body.outer_component.reduce(
                (acc, innerComponent) => {
                  const sections = innerComponent.outer_body
                    ? innerComponent.outer_body.outer_component.reduce(
                        (acc, sectionComponent) => {
                          if (sectionComponent.section) {
                            acc.push(sectionComponent.section);
                          }
                          return acc;
                        },
                        []
                      )
                    : [];
    
                  return [...acc, ...sections];
                },
                []
              ),
            },
          };
        }),
      },
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search