0

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?

DBWeinstein
  • 8,605
  • 31
  • 73
  • 118
  • 4
    That answer pretty much tells you what you need to know. You're getting an object back from `JSON.parse`. That object has a single key, `'0x123454843eacf5c500006913f04251b937db0000'`, as your log of `Object.keys` told you. So to get the value of an object's key, you use bracket notation if it's not a simple piece of text: `thisparsedJSONdata['0x123454843eacf5c500006913f04251b937db0000']`. Nowhere in the question do you mention what property you're actually after, but that answer should let you get to the rest. – Heretic Monkey Jun 01 '21 at 00:22
  • which part of your nested data you want to access ? `theparsedJSONdata` isn't an array or nor it has any `0` field. like : `{ 0 : { ... } }` – Nur Jun 01 '21 at 10:26

0 Answers0