Home > Software engineering >  JSON objects to Array keep index
JSON objects to Array keep index

Time:11-10

Hey everyone so struggling a bit here. I am receiving data from a POST api and trying to use that data to parse and update a backend MongoDB. I am receiving the data, but can't seem to parse it correctly.

{
    "0xa0cC4414019471bef0fe0b07a76dA1F7CDc4cCf7": {
        "0x6f5dabb430572370d63010e5d6a52e39f40e73751e8d49f3e0088d6b76a5dc53": {
            "chain": "bsc",
            "soldAmount": 77.91952132935356,
            "boughtAmount": 7581.592038,
            "basePrice": 0.010277461638506796,
            "usdPrice": 0.010277461638506796,
            "usdSpent": 77.91952132935356,
            "walletAddress": "0x06f74D2dB8bFbeD7bFa3f809f01D7ad4bdaCd0D0",
            "whale": false
        }
    },
    "0x81cAd0ab645A1792F585cE93c5f955Ff3eCC3951": {
        "0x58601dd284313e72c70987774129659f0f3327354e03dad820ff498ac2bcd980": {
            "chain": "bsc",
            "soldAmount": 0.04130355997068365,
            "boughtAmount": 12006668.37444406,
            "basePrice": 3.4400517014859345e-09,
            "usdPrice": 1.1540347696736513e-06,
            "usdSpent": 13.856112772049464,
            "walletAddress": "0x52739B1646EBE3879C2592686D10094a3f88D3Aa",
            "whale": false
        }
    }
}

I need to keep both keys starting with 0x, but anytime I format it I lost those key value, or can't get down to to the 3rd level down.

Goal is to the use each of these items to run against a function.

Ultimate cool would be to have a new array with all items in one.

Any help is appreciated. Below is the code I currently have.

var importData = req.body;

var txsData = [];

for (let [tx, value] of Object.entries(importData)) {
    txsData.push([tx, value])
  }

CodePudding user response:

As per my understanding, You are trying to store all the keys (nested as well) start with 0x in a separate array. If Yes, here you go :

const jsonObj = {
  "0xa0cC4414019471bef0fe0b07a76dA1F7CDc4cCf7": {
    "0x6f5dabb430572370d63010e5d6a52e39f40e73751e8d49f3e0088d6b76a5dc53": {
      "chain": "bsc",
      "soldAmount": 77.91952132935356,
      "boughtAmount": 7581.592038,
      "basePrice": 0.010277461638506796,
      "usdPrice": 0.010277461638506796,
      "usdSpent": 77.91952132935356,
      "walletAddress": "0x06f74D2dB8bFbeD7bFa3f809f01D7ad4bdaCd0D0",
      "whale": false
    }
  },
  "0x81cAd0ab645A1792F585cE93c5f955Ff3eCC3951": {
    "0x58601dd284313e72c70987774129659f0f3327354e03dad820ff498ac2bcd980": {
      "chain": "bsc",
      "soldAmount": 0.04130355997068365,
      "boughtAmount": 12006668.37444406,
      "basePrice": 3.4400517014859345e-09,
      "usdPrice": 1.1540347696736513e-06,
      "usdSpent": 13.856112772049464,
      "walletAddress": "0x52739B1646EBE3879C2592686D10094a3f88D3Aa",
      "whale": false
    }
  }
};

const res = [];

function getKeys(obj) {
  Object.keys(obj).forEach(key => {
    if (key.startsWith('0x')) res.push(key);
    if (typeof obj[key] === 'object') {
        getKeys(obj[key])
    }
  })
}

getKeys(jsonObj);
console.log(res);

  • Related