how to i access nested data in javascript object?

  arrays, javascript, json, nested

I’m working in Google Scripts.

I’m getting JSON data back from an API call:

{
  "0x123454843eacf5c500006913f04251b937db0000": {
    "products": [
      {
    "label": "Aave V2",
    "assets": [
      {
        "type": "interest-bearing",
        "category": "deposit",
        "address": "0x60D55F02A771d515e077c9C2403a1ef324885CeC",
        "symbol": "USDT",
        "decimals": 6,
        "label": "USDT in Aave",
        "img": "networks/polygon/0xc2132d05d31c914a87c6611c10748aeb04b58e8f.png",
        "protocol": "aave",
        "protocolDisplay": "Aave",
        "protocolSymbol": "AAVE",
        "price": 1,
        "apy": 0.025929545464891887,
        "balanceRaw": "672285742",
        "balance": 672.285742,
        "balanceUSD": 672.285742
      },
      {
        "type": "claimable",
        "category": "claimable",
        "address": "0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270",
        "symbol": "WMATIC",
        "decimals": 18,
        "label": "Claimable WMATIC",
        "img": "networks/polygon/0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270.png",
        "price": 1.84,
        "balance": 0.4851289898935119,
        "balanceRaw": "485128989893511846",
        "balanceUSD": 0.8926373414040619
      }
    ],
    "meta": []
  }
],
"meta": [
  {
    "label": "Total",
    "value": 673.1783793414041,
    "type": "dollar"
  },
  {
    "label": "Assets",
    "value": 673.1783793414041,
    "type": "dollar"
  },
  {
    "label": "Debt",
    "value": 0,
    "type": "dollar"
  }
]
 }

I have the following code in google scripts

var theparsedJSONdata = JSON.parse(response); // parses the JSON response from the API
Logger.log("theparsedJSONdata = " + theparsedJSONdata)
var someInfo = theparsedJSONdata[0]
Logger.log("someInfo = " + someInfo)
console.log(Object.keys(theparsedJSONdata));

I get the following output in the console

 8:06:36 PM Info    theparsedJSONdata = [object Object]
 8:06:36 PM Info    someInfo = undefined
 8:06:36 PM Info    [ '0x123454843eacf5c500006913f04251b937db0000' ]

I’ve tried several different ways to get at the nested data. Do I Have to use the wallet address as the key? Whenever I try dot notation – for example, I used

   "theparsedJSONdata[0].type"

the console throws an error saying TypeError: Cannot read property ‘type’ of undefined.

I’ve tried to use this SO answer and I can’t figure it out. What am I doing wrong? How do I access the nested data?

Source: Ask Javascript Questions

LEAVE A COMMENT