javascriptarrayspaypal-nvp

Javascript parse Response Object into Array of Objects


Below is the response from Paypal's NVP API. I am not familiar with working with a response that is a single object of keys/values like this. Typically I would expect for a search to be return in a form of an array with objects within.

Is there a way for me to turn this into an Array of Objects?

{
L_TIMESTAMP0: "2018-05-08T19:23:17Z",
L_TIMESTAMP1: "2018-05-08T18:50:01Z",
L_TIMESTAMP2: "2018-05-08T18:45:30Z",
L_TIMEZONE0: "GMT",
L_TIMEZONE1: "GMT",
L_TIMEZONE2: "GMT",
L_TYPE0: "Payment",
L_TYPE1: "Payment",
L_TYPE2: "Payment",
L_NAME0: "Person One",
L_NAME1: "Person Two",
L_NAME2: "Person Three",
L_TRANSACTIONID0: "*********",
L_TRANSACTIONID1: "*********",
L_TRANSACTIONID2: "*********",
L_STATUS0: "Completed",
L_STATUS1: "Completed",
L_STATUS2: "Completed",
L_AMT0: "10.00",
L_AMT1: "100.00",
L_AMT2: "1000.00",
L_CURRENCYCODE0: "USD",
L_CURRENCYCODE1: "USD",
L_CURRENCYCODE2: "USD",
L_FEEAMT0: "-0.29",
L_FEEAMT1: "-2.93",
L_FEEAMT2: "-29.30",
L_NETAMT0: "9.71",
L_NETAMT1: "97.70",
L_NETAMT2: "970.70",
TIMESTAMP: "2018-05-08T19:47:10Z", // not for array
CORRELATIONID: "*******", // not for array
ACK: "Success", // not for array
VERSION: "204", // not for array
BUILD: "39949200" // not for array
}

I would like parse this into an array of objects:

const recentOrders = [{
    timestamp: L_TIMESTAMP0,
    timezone: L_TIMEZONE0,
    type: L_TYPE,
    .....
},
{ timestamp: L_TIMESTAMP1, .... },
{ timestamp: L_TIMESTAMP2, .... },
// .... and so forth
]

Solution

  • Something like this should work:

    var data = {
    L_TIMESTAMP0: "2018-05-08T19:23:17Z",
    L_TIMESTAMP1: "2018-05-08T18:50:01Z",
    L_TIMESTAMP2: "2018-05-08T18:45:30Z",
    L_TIMEZONE0: "GMT",
    L_TIMEZONE1: "GMT",
    L_TIMEZONE2: "GMT",
    L_TYPE0: "Payment",
    L_TYPE1: "Payment",
    L_TYPE2: "Payment",
    L_NAME0: "Person One",
    L_NAME1: "Person Two",
    L_NAME2: "Person Three",
    L_TRANSACTIONID0: "*********",
    L_TRANSACTIONID1: "*********",
    L_TRANSACTIONID2: "*********",
    L_STATUS0: "Completed",
    L_STATUS1: "Completed",
    L_STATUS2: "Completed",
    L_AMT0: "10.00",
    L_AMT1: "100.00",
    L_AMT2: "1000.00",
    L_CURRENCYCODE0: "USD",
    L_CURRENCYCODE1: "USD",
    L_CURRENCYCODE2: "USD",
    L_FEEAMT0: "-0.29",
    L_FEEAMT1: "-2.93",
    L_FEEAMT2: "-29.30",
    L_NETAMT0: "9.71",
    L_NETAMT1: "97.70",
    L_NETAMT2: "970.70",
    TIMESTAMP: "2018-05-08T19:47:10Z", // not for array
    CORRELATIONID: "*******", // not for array
    ACK: "Success", // not for array
    VERSION: "204", // not for array
    BUILD: "39949200" // not for array
    };
    
    const recentOrders = [];
    
    var keys = Object.keys(data).filter(r => r.startsWith('L_'));
    
    keys.forEach(k => {
        var index = parseInt(k.replace(/\D/g,''));
        var newKey = k.substring(2).replace(/[0-9]/g, '').toLowerCase();
        if (recentOrders[index] === undefined) {
            recentOrders[index] = {};
        }
        recentOrders[index][newKey] = data[k];
    });
    
    console.log(recentOrders);