0

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.

1 Answer 1

1

You'll want to create a new array of objects, then loop through the original array and add entries to the new array based on that. For example --

const input = [
  {
    "key": "guid",
    "parent": "id__guid"
  },
  {
    "key": "version",
    "parent": "id__version"
  }
  // et cetera... your input
];

// these are buckets.
const transformedObject = {
  "Identification": []
}

// First we put the data into the right buckets
input.forEach((entry) => {

  // Create a new object with the key
  const newObject = { key: entry.key };

  // By default, the parent seems to be "Identification"
  let parentKey = "Identification";

  // Find out if the parent's name has an underscore?
  if (entry.parent.split("__").length > 1) {

    // If so, that's the new parent
    parentKey = entry.parent.split("__")[0];

  }

  // If there isn't an array for this parent, make one
  if (!transformedObject[parentKey]) {
    transformedObject[parentKey] = [];
  }

  transformedObject[parentKey].push(newObject);  

})

const output = [];

// Then we need to shape the data
Object.keys(transformedObject).forEach((parentKey) => {
  const parentGroup = {
    name: parentKey,
    fields: transformedObject[parentKey]
  };
  output.push(parentGroup);
});


console.log(output);

You'll notice this doesn't get you all the way there. The "id" prefix seems to be merged into the "Identification" prefix, and you want to keep the parent value on some of these objects. You'll need some conditionals or a map or something to get that part working. But I hope this is a start!

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Thank you for reply. I checked this code. This code is working when we have parent property 2 level, but if it is more than that, it is not working. e.g if we have parent like this OwnerAffiliates__Correspondents__Correspondent__Name it is taking only OwnerAffiliates and Name and skipping Correspondents and Correspondent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.