简体   繁体   中英

How to efficiently create nested json from another json object

I am trying to execute an sql query and from the result I want to create a nested json object. I am getting json response like this after executing the query using sequelize. From this json I want to create a nested json.

[
  {
    uid: 'RBZ7K2122715',
    subscriberName: 'sub1',
    productName: 'PROD1',
    actiondate: 2020-10-06T02:00:00.000Z,
    enrolStatus: 'ACTIVE',
    lastModifiedTime: 2020-10-07T19:41:54.000Z
  },
  {
    uid: 'RBZ7K2122715',
    subscriberName: 'sub1',
    productName: 'PROD2',
    actiondate: 2020-10-06T02:00:00.000Z,
    enrolStatus: 'ACTIVE',
    lastModifiedTime: 2020-10-07T19:42:44.000Z
  },
  {
    uid: 'RBZ7K2123333',
    subscriberName: 'sub2',
    productName: 'PROD2',
    actiondate: 2020-07-01T08:32:00.000Z,
    enrolStatus: 'NOT-ACTIVE',
    lastModifiedTime: 2020-10-14T13:49:37.000Z
  },
  {
    uid: 'RBZ7K2123333',
    subscriberName: 'sub2',
    productName: 'PROD1',
    actiondate: 2020-04-01T03:32:00.000Z,
    enrolStatus: 'ACTIVE',
    lastModifiedTime: 2020-10-14T13:50:08.000Z
  },
  {
    uid: 'RBZ7K2122715',
    subscriberName: 'sub2',
    productName: 'PROD2',
    actiondate: 2020-03-12T08:32:00.000Z,
    enrolStatus: 'ACTIVE',
    lastModifiedTime: 2020-10-30T12:07:55.000Z
  }
]

I want to create a nested json object like this. Can anyone please help here to achieve it efficiently?

[
      {
        "uid": "RBZ7K2122715",
        "enrollment-status" : [
                                    {
                                        "subscriberName" : "sub1",
                                        "productName" : "PROD2",
                                        "actionDate" :  "2020-03-12 08:32:00",
                                        "status" : "ACTIVE",
                                        "lastModifiedTime" : "2020-10-30 12:07"
                                    },
                                    {
                                        "subscriberName" : "sub2",
                                        "productName" : "PROD1",
                                        "actionDate" :  "2020-10-06 02:00:00",
                                        "status" : "ACTIVE",
                                        "lastModifiedTime" : "2020-10-14 13:49"
                                    },
                                    {
                                        "subscriberName" : "sub2",
                                        "productName" : "PROD2",
                                        "actionDate" :  "2020-10-06 02:00:00",
                                        "status" : "ACTIVE",
                                        "lastModifiedTime" : "2020-10-14 13:49"
                                    }
                               ]
      },
      {
        "uid": "RBZ7K2123333",
        "enrollment-status" : [
                                    {
                                        "subscriberName" : "sub1",
                                        "productName" : "PROD2",
                                        "actionDate" :  "2020-07-01 08:32:00",
                                        "status" : "NOT-ACTIVE",
                                        "lastModifiedTime" : "2020-10-14 13:49"
                                    },
                                    {
                                        "subscriberName" : "sub1",
                                        "productName" : "PROD1",
                                        "actionDate" :  "2020-04-01 03:32:00",
                                        "status" : "ACTIVE",
                                        "lastModifiedTime" : "2020-10-14 13:49"
                                    }
                              ]
      }
]

You can try the following code

queryResult.reduce((a, {
  uid, subscriberName, productName, actiondate, enrolStatus, lastModifiedTime,
}) => {
  const indexOfSameUid = a.findIndex((v) => v.uid === uid);
  const currentEnrollmentStatus = {
    subscriberName, productName, actionDate: actiondate, status: enrolStatus, lastModifiedTime,
  };
  if (indexOfSameUid === -1) {
    a.push({
      uid,
      'enrollment-status': [currentEnrollmentStatus],
    });
    return a;
  }
  a[indexOfSameUid]['enrollment-status'].push(currentEnrollmentStatus);
  return a;
}, []);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM