Skip to main content
Source Link
Piyush Jain
  • 2k
  • 3
  • 22
  • 40

Javascript json into nested json

I have a huge json which I am fetching from Excel sheet. Data I am getting as array of objects and one object looks like below.

[
  {
    "key": "guid",
    "parent": "id__guid"
  },
  {
    "key": "version",
    "parent": "id__version"
  },
  {
    "key": "register",
    "parent": "register"
  },
  {
    "key": "offloadId",
    "parent": "offloadId"
  },
  {
    "key": "action",
    "parent": "action"
  },
  {
    "key": "reported",
    "parent": "reported"
  },
  {
    "key": "control",
    "parent": "control"
  },
  {
    "key": "AppNum",
    "parent": "Identification__AppNum"
  },
  {
    "key": "DataTp",
    "parent": "Identification__DataTp"
  },
  {
    "key": "DtOgWatchDt",
    "parent": "Identification__DtOgWatchDt"
  },
  {
    "key": "DtPendingWatchDt",
    "parent": "Identification__DtPendingWatchDt"
  },
  {
    "key": "IssRef",
    "parent": "Identification__IssRef"
  },
  {
    "key": "ImgRef",
    "parent": "Identification__ImgRef"
  },
  {
    "key": "Register",
    "parent": "Identification__Register"
  },
  {
    "key": "-",
    "parent": "Identification__ImgRefFullPub"
  },
  {
    "key": "DtAppDt",
    "parent": "Dates__DtAppDt"
  },
  {
    "key": "IdxNam",
    "parent": "Description__IdxNam"
  },
  {
    "key": "Clms",
    "parent": "Description__Clms"
  },
  {
    "key": "Disclaims",
    "parent": "Description__Disclaims"
  },
  {
    "key": "LglStsCd",
    "parent": "Status__LglStsCd"
  },
  {
    "key": "UsPtoStsCd",
    "parent": "Status__UsPtoStsCd"
  },
  {
    "key": "PtoStsCdDt",
    "parent": "Status__PtoStsCdDt"
  },
  {
    "key": "StsFlag",
    "parent": "Status__StsFlag"
  },
  {
    "key": "SrcInd",
    "parent": "Status__SrcInd"
  },
  {
    "key": "LglStsCdNorm",
    "parent": "Status__LglStsCdNorm"
  }
]

I want to convert it into this format which is nested json.

[
  {
    name: "Identification",
    fields: [
      {
        "key": "AppNum",
        "parent": "Identification__AppNum"
      },
      {
        "key": "DataTp",
        "parent": "Identification__DataTp"
      },
      {
        "key": "DtOgWatchDt"
      },
      {
        "key": "DtPendingWatchDt"
      },
      {
        "key": "IssRef"
      },
      {
        "key": "ImgRef"
      },
      {
        "key": "Register"
      },
      {
        "key": "ImgRefFullPub"
      },
      {
        "key": "guid"
      },
      {
        "key": "version"
      },
      {
        "key": "offloadid"
      },
      {
        "key": "reported"
      },
      {
        "key": "control"
      }
    ]
  },
  {
    name: "Description",
    fields: [
      {
        "key": "IdxNam"
      },
      {
        "key": "Clms"
      },
      {
        "key": "Disclaims"
      }
    ]
  },
  {
    name: "Status",
    fields: [
      {
        "key": "UsPtoStsCd"
      },
      {
        "key": "PtoStsCdDt"
      },
      {
        "key": "LglStsCd"
      },
      {
        "key": "StsFlag"
      },
      {
        "key": "SrcInd"
      },
      {
        "key": "LglStsCdNorm"
      }
    ]
  },
  {
    name: "Dates",
    fields: [
      {
        "key": "DtAppDt"
      }
    ]
  }
]

As you can see according to parent key we have to create nested structure. I have tried all the ways, I search a lot on google also, but hard luck.

Any help will be appreciated.