-5

I have JSON data as below:

    "Positions": [
            {
              "CRVL_CMB_AMT": 0,
              "CRVL_PERUT_CMB_AMT": null,
              "ISS_ID": "USD",
              "LOCAL_CURR_CDE": "USD",
              "QUANTITY": -4739568084,
              "UT_PRC_CMB_AMT": 100,
              "VALVAL_CMB_AMT": -4739568084,
              "VALVAL_ALT_CMB_AMT": -4739568084,
              "Instrument": {
                "ISS_NME": "US DOLLAR",
                "ISS_TYP": "CASH",
                "ASSET_CLASS_MNEM": "BALANCE"
              }
            },
            {
              "CRVL_CMB_AMT": 0,
              "CRVL_PERUT_CMB_AMT": null,
              "ISS_ID": "REPO-USD-WF",
              "LOCAL_CURR_CDE": "USD",
              "QUANTITY": -770000,
              "UT_PRC_CMB_AMT": 100,
              "VALVAL_CMB_AMT": -770122,
              "VALVAL_ALT_CMB_AMT": -770122,
              "Instrument": {
                "ISS_NME": "REPURCHASE AGREEMENT -  USD WF",
                "ISS_TYP": "REPO",
                "ASSET_CLASS_MNEM": "CURR EQUIV"
              }
            },
            {
              "CRVL_CMB_AMT": 0,
              "CRVL_PERUT_CMB_AMT": null,
              "ISS_ID": "REPO-USD-GS",
              "LOCAL_CURR_CDE": "USD",
              "QUANTITY": -770000,
              "UT_PRC_CMB_AMT": 100,
              "VALVAL_CMB_AMT": -770122,
              "VALVAL_ALT_CMB_AMT": -770122,
              "Instrument": {
                "ISS_NME": "REPURCHASE AGREEMENT -  USD GS",
                "ISS_TYP": "REPO",
                "ASSET_CLASS_MNEM": "CURR EQUIV"
              }
            }
]

I would like to convert this JSON to :

    [{
              "CRVL_CMB_AMT": 0,
              "CRVL_PERUT_CMB_AMT": null,
              "ISS_ID": "USD",
              "LOCAL_CURR_CDE": "USD",
              "QUANTITY": -4739568084,
              "UT_PRC_CMB_AMT": 100,
              "VALVAL_CMB_AMT": -4739568084,
              "VALVAL_ALT_CMB_AMT": -4739568084,          
              "ISS_NME": "US DOLLAR",
              "ISS_TYP": "CASH",
              "ASSET_CLASS_MNEM": "BALANCE"              
            },
{
                  "CRVL_CMB_AMT": 0,
                  "CRVL_PERUT_CMB_AMT": null,
                  "ISS_ID": "REPO-USD-WF",
                  "LOCAL_CURR_CDE": "USD",
                  "QUANTITY": -770000,
                  "UT_PRC_CMB_AMT": 100,
                  "VALVAL_CMB_AMT": -770122,
                  "VALVAL_ALT_CMB_AMT": -770122,
                    "ISS_NME": "REPURCHASE AGREEMENT -  USD WF",
                    "ISS_TYP": "REPO",
                    "ASSET_CLASS_MNEM": "CURR EQUIV"
                },
                {
                  "CRVL_CMB_AMT": 0,
                  "CRVL_PERUT_CMB_AMT": null,
                  "ISS_ID": "REPO-USD-GS",
                  "LOCAL_CURR_CDE": "USD",
                  "QUANTITY": -770000,
                  "UT_PRC_CMB_AMT": 100,
                  "VALVAL_CMB_AMT": -770122,
                  "VALVAL_ALT_CMB_AMT": -770122,
                    "ISS_NME": "REPURCHASE AGREEMENT -  USD GS",
                    "ISS_TYP": "REPO",
                    "ASSET_CLASS_MNEM": "CURR EQUIV"
                }]

Is there any efficient way to flatten this. Length of array can be very large in 10 thousands.

Thanks

Amit
  • 1,460
  • 1
  • 14
  • 39
  • don't understood what you mean flatten here. if you want to create array from Position then you can ```[...obj.Positions]``` – Dipak Telangre May 11 '20 at 06:54
  • Each element of positions array have key Like Instruments which is object itself Since I want to use this in Grid, I need to get the key value from inside Intruments at the same level. – Amit May 11 '20 at 06:59
  • Does this answer your question? [Keeping only certain properties in a JavaScript object](https://stackoverflow.com/questions/22202766/keeping-only-certain-properties-in-a-javascript-object) – Shlok Nangia May 11 '20 at 07:01

1 Answers1

1

If it's a static key, just extract Instrument and spread it to the object:

const data = {"Positions":[{"CRVL_CMB_AMT":0,"CRVL_PERUT_CMB_AMT":null,"ISS_ID":"USD","LOCAL_CURR_CDE":"USD","QUANTITY":-4739568084,"UT_PRC_CMB_AMT":100,"VALVAL_CMB_AMT":-4739568084,"VALVAL_ALT_CMB_AMT":-4739568084,"Instrument":{"ISS_NME":"US DOLLAR","ISS_TYP":"CASH","ASSET_CLASS_MNEM":"BALANCE"}},{"CRVL_CMB_AMT":0,"CRVL_PERUT_CMB_AMT":null,"ISS_ID":"REPO-USD-WF","LOCAL_CURR_CDE":"USD","QUANTITY":-770000,"UT_PRC_CMB_AMT":100,"VALVAL_CMB_AMT":-770122,"VALVAL_ALT_CMB_AMT":-770122,"Instrument":{"ISS_NME":"REPURCHASE AGREEMENT -  USD WF","ISS_TYP":"REPO","ASSET_CLASS_MNEM":"CURR EQUIV"}},{"CRVL_CMB_AMT":0,"CRVL_PERUT_CMB_AMT":null,"ISS_ID":"REPO-USD-GS","LOCAL_CURR_CDE":"USD","QUANTITY":-770000,"UT_PRC_CMB_AMT":100,"VALVAL_CMB_AMT":-770122,"VALVAL_ALT_CMB_AMT":-770122,"Instrument":{"ISS_NME":"REPURCHASE AGREEMENT -  USD GS","ISS_TYP":"REPO","ASSET_CLASS_MNEM":"CURR EQUIV"}}]};

const res = Object.values(data)[0].map(({ Instrument, ...o }) => ({ ...Instrument, ...o }));

console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }

To do this dynamically and detect which items are objects, you'd need to iterate through each property:

const data = {"Positions":[{"CRVL_CMB_AMT":0,"CRVL_PERUT_CMB_AMT":null,"ISS_ID":"USD","LOCAL_CURR_CDE":"USD","QUANTITY":-4739568084,"UT_PRC_CMB_AMT":100,"VALVAL_CMB_AMT":-4739568084,"VALVAL_ALT_CMB_AMT":-4739568084,"Instrument":{"ISS_NME":"US DOLLAR","ISS_TYP":"CASH","ASSET_CLASS_MNEM":"BALANCE"}},{"CRVL_CMB_AMT":0,"CRVL_PERUT_CMB_AMT":null,"ISS_ID":"REPO-USD-WF","LOCAL_CURR_CDE":"USD","QUANTITY":-770000,"UT_PRC_CMB_AMT":100,"VALVAL_CMB_AMT":-770122,"VALVAL_ALT_CMB_AMT":-770122,"Instrument":{"ISS_NME":"REPURCHASE AGREEMENT -  USD WF","ISS_TYP":"REPO","ASSET_CLASS_MNEM":"CURR EQUIV"}},{"CRVL_CMB_AMT":0,"CRVL_PERUT_CMB_AMT":null,"ISS_ID":"REPO-USD-GS","LOCAL_CURR_CDE":"USD","QUANTITY":-770000,"UT_PRC_CMB_AMT":100,"VALVAL_CMB_AMT":-770122,"VALVAL_ALT_CMB_AMT":-770122,"Instrument":{"ISS_NME":"REPURCHASE AGREEMENT -  USD GS","ISS_TYP":"REPO","ASSET_CLASS_MNEM":"CURR EQUIV"}}]};

const res = Object.values(data)[0].map(e => {
  Object.entries(e).forEach(([k, v]) => {
    if (v && typeof v == "object" && !v.map) {
      e = { ...e, ...v };
      delete e[k];
    }
  });
  return e;
});

console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }

The above will work for any nested object at a single depth.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79