Home > Net >  Is there is any way I can create a Array from a JSON object using javaScript
Is there is any way I can create a Array from a JSON object using javaScript

Time:10-07

Is there is any way I can create a Array from a JSON object using javaScript

My JSON:

value: {
   
    "getAllProvidersoriginal##providers[0].id": "102",
    "getAllProvidersoriginal##providers[0].provider_first_name": "Varun",
    "getAllProvidersoriginal##providers[0].provider_last_name": "Sengupta",
    "getAllProvidersoriginal##providers[0].provider_email":
      "[email protected]",
    "getAllProvidersoriginal##providers[0].availability": "AVAILABLE",
    "getAllProvidersoriginal##providers[0].status": "active",
    "getAllProvidersoriginal##providers[0].agent_id": "A607093",
    "getAllProvidersoriginal##providers[0].created_by_user_id": "1",
    "getAllProvidersoriginal##providers[0].last_updated_by_user_id": "1",
    "getAllProvidersoriginal##providers[0].current_serving_queue_id": "0",
    "getAllProvidersoriginal##providers[0].current_serving_room_id": "0",
    "getAllProvidersoriginal##providers[0].room_id": "1",
    "getAllProvidersoriginal##providers[0].assinged_queue[0].id": "726",
    "getAllProvidersoriginal##providers[0].assinged_queue[0].queue_id": "1",
    "getAllProvidersoriginal##providers[0].assinged_queue[0].room_id": "1",
    "getAllProvidersoriginal##providers[0].assinged_queue[0].provider_id":
      "102",
    "getAllProvidersoriginal##providers[0].assinged_queue[0].status": "active",
    "getAllProvidersoriginal##providers[0].assinged_queue[0].created_on":
      "2022-10-06T17:44:13.000Z",
    "getAllProvidersoriginal##providers[0].assinged_queue[0].updated_on":
      "2022-10-06T17:44:13.000Z",
    "getAllProvidersoriginal##providers[0].assinged_queue[0].queue_name":
      "Adjudication",
    "getAllProvidersoriginal##providers[0].assinged_queue[0].queue_description":
      "Adjudication",
    "getAllProvidersoriginal##providers[0].assinged_queue[0].room_name": "DPIE",
    "getAllProvidersoriginal##providers[0].assinged_queue[1].id": "727",
    "getAllProvidersoriginal##providers[0].assinged_queue[1].queue_id": "2",
    "getAllProvidersoriginal##providers[0].assinged_queue[1].room_id": "1",
    "getAllProvidersoriginal##providers[0].assinged_queue[1].provider_id":
      "102",
    "getAllProvidersoriginal##providers[0].assinged_queue[1].status": "active",
    "getAllProvidersoriginal##providers[0].assinged_queue[1].created_on":
      "2022-10-06T17:44:13.000Z",
    "getAllProvidersoriginal##providers[0].assinged_queue[1].updated_on":
      "2022-10-06T17:44:13.000Z",
    "getAllProvidersoriginal##providers[0].assinged_queue[1].queue_name":
      "Interview",
    "getAllProvidersoriginal##providers[0].assinged_queue[1].queue_description":
      "Interview",
    "getAllProvidersoriginal##providers[0].assinged_queue[1].room_name": "DPIE",
    "getAllProvidersoriginal##providers[0].assinged_queue[2].id": "728",
    "getAllProvidersoriginal##providers[0].assinged_queue[2].queue_id": "3",
    "getAllProvidersoriginal##providers[0].assinged_queue[2].room_id": "1",
    "getAllProvidersoriginal##providers[0].assinged_queue[2].provider_id":
      "102",
    "getAllProvidersoriginal##providers[0].assinged_queue[2].status": "active",
    "getAllProvidersoriginal##providers[0].assinged_queue[2].created_on":
      "2022-10-06T17:44:13.000Z",
    "getAllProvidersoriginal##providers[0].assinged_queue[2].updated_on":
      "2022-10-06T17:44:13.000Z",
    "getAllProvidersoriginal##providers[0].assinged_queue[2].queue_name":
      "Oath Administration",
    "getAllProvidersoriginal##providers[0].assinged_queue[2].queue_description":
      "Oath Administration",
    "getAllProvidersoriginal##providers[0].assinged_queue[2].room_name": "DPIE",

The Array and Object I need after cleanup

{
    "id": 102,
    "provider_first_name": "Varun",
    "provider_last_name": "Sengupta",
    "provider_email": "[email protected]",
    "availability": "AVAILABLE",
    "status": "active",
    "agent_id": "A607093",
    "created_by_user_id": 1,
    "last_updated_by_user_id": 1,
    "current_serving_queue_id": 0,
    "current_serving_room_id": 0,
    "room_id": 1,
    "assinged_queue": [
        {
            "id": 726,
            "queue_id": 1,
            "room_id": 1,
            "provider_id": 102,
            "status": "active",
            "created_on": "2022-10-06T17:44:13.000Z",
            "updated_on": "2022-10-06T17:44:13.000Z",
            "queue_name": "Adjudication",
            "queue_description": "Adjudication",
            "room_name": "DPIE"
        },
        {
            "id": 727,
            "queue_id": 2,
            "room_id": 1,
            "provider_id": 102,
            "status": "active",
            "created_on": "2022-10-06T17:44:13.000Z",
            "updated_on": "2022-10-06T17:44:13.000Z",
            "queue_name": "Interview",
            "queue_description": "Interview",
            "room_name": "DPIE"
        },
        {
            "id": 728,
            "queue_id": 3,
            "room_id": 1,
            "provider_id": 102,
            "status": "active",
            "created_on": "2022-10-06T17:44:13.000Z",
            "updated_on": "2022-10-06T17:44:13.000Z",
            "queue_name": "Oath Administration",
            "queue_description": "Oath Administration",
            "room_name": "DPIE"
        }
    ]
}

I tried using this


let apiGetWaiting = ProviderOriginal["value"]

let ApiKeyGetWaiting =[];
for (const key of Object.keys(apiGetWaiting)) {
    let indexArr = key.match(/^.*?\[[^\d]*(\d )[^\d]*\].*$/);
    let index = indexArr ? Number(indexArr[1]) : -1
    let indexKey = key.split('.')[1];
    let obj = {};
    if (index > -1) {
        if (ApiKeyGetWaiting[index]) {
            ApiKeyGetWaiting[index][indexKey] = apiGetWaiting[key];
        } else {
            obj[indexKey] = apiGetWaiting[key]
            ApiKeyGetWaiting[index] = obj;
        }
    }
}

console.log(ApiKeyGetWaiting);

but this code just returning me my parent Array Object, but I want both parent and Child Array as I share

Can anyone Help me cleanup my JSON and Return a clean Array Object

CodePudding user response:

You can make use of two reduce loops where you will copy the reference of accumulator & pass it into the inner reduce function & modify/add the data as per the requirement. Also it will work for n-level nested array data.

const value= {"getAllProvidersoriginal##providers[0].id": "102", "getAllProvidersoriginal##providers[0].provider_first_name": "Varun",     "getAllProvidersoriginal##providers[0].provider_last_name": "Sengupta",     "getAllProvidersoriginal##providers[0].provider_email":       "[email protected]",     "getAllProvidersoriginal##providers[0].availability": "AVAILABLE",     "getAllProvidersoriginal##providers[0].status": "active",     "getAllProvidersoriginal##providers[0].agent_id": "A607093",     "getAllProvidersoriginal##providers[0].created_by_user_id": "1",     "getAllProvidersoriginal##providers[0].last_updated_by_user_id": "1",     "getAllProvidersoriginal##providers[0].current_serving_queue_id": "0",     "getAllProvidersoriginal##providers[0].current_serving_room_id": "0",     "getAllProvidersoriginal##providers[0].room_id": "1",     "getAllProvidersoriginal##providers[0].assinged_queue[0].id": "726",     "getAllProvidersoriginal##providers[0].assinged_queue[0].queue_id": "1",     "getAllProvidersoriginal##providers[0].assinged_queue[0].room_id": "1",     "getAllProvidersoriginal##providers[0].assinged_queue[0].provider_id":       "102",     "getAllProvidersoriginal##providers[0].assinged_queue[0].status": "active",     "getAllProvidersoriginal##providers[0].assinged_queue[0].created_on":       "2022-10-06T17:44:13.000Z",     "getAllProvidersoriginal##providers[0].assinged_queue[0].updated_on":       "2022-10-06T17:44:13.000Z",     "getAllProvidersoriginal##providers[0].assinged_queue[0].queue_name":       "Adjudication",     "getAllProvidersoriginal##providers[0].assinged_queue[0].queue_description":       "Adjudication",     "getAllProvidersoriginal##providers[0].assinged_queue[0].room_name": "DPIE",     "getAllProvidersoriginal##providers[0].assinged_queue[1].id": "727",     "getAllProvidersoriginal##providers[0].assinged_queue[1].queue_id": "2",     "getAllProvidersoriginal##providers[0].assinged_queue[1].room_id": "1",     "getAllProvidersoriginal##providers[0].assinged_queue[1].provider_id":       "102",     "getAllProvidersoriginal##providers[0].assinged_queue[1].status": "active",     "getAllProvidersoriginal##providers[0].assinged_queue[1].created_on":       "2022-10-06T17:44:13.000Z",     "getAllProvidersoriginal##providers[0].assinged_queue[1].updated_on":       "2022-10-06T17:44:13.000Z",     "getAllProvidersoriginal##providers[0].assinged_queue[1].queue_name":       "Interview",     "getAllProvidersoriginal##providers[0].assinged_queue[1].queue_description":       "Interview",     "getAllProvidersoriginal##providers[0].assinged_queue[1].room_name": "DPIE",     "getAllProvidersoriginal##providers[0].assinged_queue[2].id": "728",     "getAllProvidersoriginal##providers[0].assinged_queue[2].queue_id": "3",     "getAllProvidersoriginal##providers[0].assinged_queue[2].room_id": "1",     "getAllProvidersoriginal##providers[0].assinged_queue[2].provider_id":       "102",     "getAllProvidersoriginal##providers[0].assinged_queue[2].status": "active",     "getAllProvidersoriginal##providers[0].assinged_queue[2].created_on":       "2022-10-06T17:44:13.000Z",     "getAllProvidersoriginal##providers[0].assinged_queue[2].updated_on":       "2022-10-06T17:44:13.000Z",     "getAllProvidersoriginal##providers[0].assinged_queue[2].queue_name":       "Oath Administration",     "getAllProvidersoriginal##providers[0].assinged_queue[2].queue_description":       "Oath Administration",     "getAllProvidersoriginal##providers[0].assinged_queue[2].room_name": "DPIE"  }

const format = obj => Object.keys(obj).reduce((acc, elem) => {
    const [_, prop] = elem.split('##providers');
    const allKeys = prop.match(/\d /g).map(Number);
    const val = prop.split('[').flatMap(o => o.split('.')[1] || []);
    allKeys.reduce((a, e, j, s) => {
        if (j == s.length - 1) {
            (a[e] ??= {})[val[j]] = value[elem];
            return a;
        } else {
            (a[e] ??= {})[val[j]] ??= [];
            return a[e][val[j]];
        };
    }, acc);
    return acc;
}, []);

console.log(format(value));

CodePudding user response:

Lodash, if you don't mind.

Live Demo:

const source = {"getAllProvidersoriginal##providers[0].id":"102","getAllProvidersoriginal##providers[0].provider_first_name":"Varun","getAllProvidersoriginal##providers[0].provider_last_name":"Sengupta","getAllProvidersoriginal##providers[0].provider_email":"[email protected]","getAllProvidersoriginal##providers[0].availability":"AVAILABLE","getAllProvidersoriginal##providers[0].status":"active","getAllProvidersoriginal##providers[0].agent_id":"A607093","getAllProvidersoriginal##providers[0].created_by_user_id":"1","getAllProvidersoriginal##providers[0].last_updated_by_user_id":"1","getAllProvidersoriginal##providers[0].current_serving_queue_id":"0","getAllProvidersoriginal##providers[0].current_serving_room_id":"0","getAllProvidersoriginal##providers[0].room_id":"1","getAllProvidersoriginal##providers[0].assinged_queue[0].id":"726","getAllProvidersoriginal##providers[0].assinged_queue[0].queue_id":"1","getAllProvidersoriginal##providers[0].assinged_queue[0].room_id":"1","getAllProvidersoriginal##providers[0].assinged_queue[0].provider_id":"102","getAllProvidersoriginal##providers[0].assinged_queue[0].status":"active","getAllProvidersoriginal##providers[0].assinged_queue[0].created_on":"2022-10-06T17:44:13.000Z","getAllProvidersoriginal##providers[0].assinged_queue[0].updated_on":"2022-10-06T17:44:13.000Z","getAllProvidersoriginal##providers[0].assinged_queue[0].queue_name":"Adjudication","getAllProvidersoriginal##providers[0].assinged_queue[0].queue_description":"Adjudication","getAllProvidersoriginal##providers[0].assinged_queue[0].room_name":"DPIE","getAllProvidersoriginal##providers[0].assinged_queue[1].id":"727","getAllProvidersoriginal##providers[0].assinged_queue[1].queue_id":"2","getAllProvidersoriginal##providers[0].assinged_queue[1].room_id":"1","getAllProvidersoriginal##providers[0].assinged_queue[1].provider_id":"102","getAllProvidersoriginal##providers[0].assinged_queue[1].status":"active","getAllProvidersoriginal##providers[0].assinged_queue[1].created_on":"2022-10-06T17:44:13.000Z","getAllProvidersoriginal##providers[0].assinged_queue[1].updated_on":"2022-10-06T17:44:13.000Z","getAllProvidersoriginal##providers[0].assinged_queue[1].queue_name":"Interview","getAllProvidersoriginal##providers[0].assinged_queue[1].queue_description":"Interview","getAllProvidersoriginal##providers[0].assinged_queue[1].room_name":"DPIE","getAllProvidersoriginal##providers[0].assinged_queue[2].id":"728","getAllProvidersoriginal##providers[0].assinged_queue[2].queue_id":"3","getAllProvidersoriginal##providers[0].assinged_queue[2].room_id":"1","getAllProvidersoriginal##providers[0].assinged_queue[2].provider_id":"102","getAllProvidersoriginal##providers[0].assinged_queue[2].status":"active","getAllProvidersoriginal##providers[0].assinged_queue[2].created_on":"2022-10-06T17:44:13.000Z","getAllProvidersoriginal##providers[0].assinged_queue[2].updated_on":"2022-10-06T17:44:13.000Z","getAllProvidersoriginal##providers[0].assinged_queue[2].queue_name":"Oath Administration","getAllProvidersoriginal##providers[0].assinged_queue[2].queue_description":"Oath Administration","getAllProvidersoriginal##providers[0].assinged_queue[2].room_name":"DPIE"}

const obj = _.transform(source, (acc, value, key) => {
    const path = key.replace('getAllProvidersoriginal##providers[0].', '');
    return _.set(acc, path, value);
}, {});

console.log(obj);
.as-console-wrapper { max-height: 100% !important; top: 0 }
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

  • Related