0

I have been trying to figure this out for hours. I've seen this SO question and I still cannot figure this out.

I have some Jason data that I know begins like this:

{
  "0x123454843eacf5c5318e1234504251b937d12345": [
{
  "poolIndex": 0,
  "stakingStrategy": "masterchef",
  "farmName": "sushiChef",
 ....

I've written the following to get at the information like "poolIndex" and "stakingStrategy":

  function parseTheDataFunctionSushi(walletAddress, networkName){
  // calls a funciton to pull and parse the api data

    var walletAddress = "0x123454843eacf5c5318e1234504251b937d12345";
    var networkName = "polygon";

    var theparsedJSONdata = pullAndParseAPISushi(walletAddress, networkName)
    console.log("object keys are " + Object.keys(walletAddress)); 
    var firstCrack = theparsedJSONdata[walletAddress][0]['poolIndex']
    console.log("firstCrack is " + firstCrack)

This does not work. I've written firstCrack every way I can think of

  theparsedJSONdata[walletAddress].poolIndex
  theparsedJSONdata[walletAddress][0].poolIndex

None of them work. So frustrating. Any help would be appreciated.

For what it's worth, `Object.keys(walletAddress) returns

 object keys are 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41

Here's the other function:

  function pullAndParseAPISushi (walletAddress, networkName){

   console.log("walletAddress inside pullAndParseAPISushi is = " + walletAddress);
   console.log("network is " + networkName);
   var apiKEY = "96e0cc51-a62e-42ca-acee-910ea7d2a241";  // API key from Zapper
   var url = "https://api.zapper.fi/v1/staked-balance/masterchef?addresses%5B%5D="+ walletAddress + "&network=" + networkName + "&api_key=" + apiKEY;
   // assembles the API URL with the wallet addressa, network and name
   console.log("url is " + URL);
   var response = UrlFetchApp.fetch(url); // pulls data from the API
   console.log(response)
   var theparsedJSONdata = JSON.parse(response); // parses the JSON response from the API
   console.log(theparsedJSONdata)
   return theparsedJSONdata
 }
Iamblichus
  • 18,540
  • 2
  • 11
  • 27
DBWeinstein
  • 8,605
  • 31
  • 73
  • 118
  • Consider undeleting your related question [Do I have to do something to an argument coming from a cell in Google Sheets?](https://stackoverflow.com/questions/67794290/do-i-have-to-do-something-to-an-argument-coming-from-a-cell-in-google-sheets?noredirect=1#comment119830254_67794290) See [Access deleted questions](https://meta.stackoverflow.com/a/366500/13045193). – doubleunary Jun 02 '21 at 07:16
  • This is not reproducible. If the returned JSON and the code are exactly as you mentioned, `theparsedJSONdata[walletAddress][0].poolIndex` should return 0. Can you try logging `theparsedJSONdata` right before calling `theparsedJSONdata[walletAddress][0]['poolIndex']`? What do you get? – Iamblichus Jun 02 '21 at 07:16

2 Answers2

0

Have you tried?

theparsedJSONdata.walletAddress[0].poolIndex

don't have enough rep to make a comment but I think this might work, let me know!

0

If the data structure is indeed as you specified, then one of the attempts you tried should work. See below for a functional example.

const data = {
  "0x123454843eacf5c5318e1234504251b937d12345": [
    {
      "poolIndex": 0,
      "stakingStrategy": "masterchef",
      "farmName": "sushiChef",
    }
  ]
}

const walletAddress = "0x123454843eacf5c5318e1234504251b937d12345";

console.log(data[walletAddress][0].poolIndex);

console.log(data[walletAddress][0].stakingStrategy);
Nick
  • 16,066
  • 3
  • 16
  • 32
  • Thank you! Stil not working. I've added this `console.log(theparsedJSONdata[walletAddress][0].stakingStrategy);` and I get the following error: `TypeError: Cannot read property '0' of undefined parseTheDataFunctionSushi` – DBWeinstein Jun 02 '21 at 02:41
  • is `pullAndParseAPISushi(walletAddress, networkName)` doing an asynchronous action like fetching data? – Nick Jun 02 '21 at 02:43
  • I'm in google sheets trying to pull data from Zapper API to track positions in Sushiswap. – DBWeinstein Jun 02 '21 at 02:45
  • If you `console.log(theparsedJSONdata)` directly, what do you get? – Nick Jun 02 '21 at 02:47
  • { '0x123454843eacf5c5318e1234504251b937d12345': [ { poolIndex: 0, stakingStrategy: 'masterchef', farmName: 'sushiChef', rewardAddress: '0x0000000', tokenAddress: '0x000000', rewardTokenSymbol: 'SUSHI', rewardTokenDecimals: 18, rewardTokenAddress: '0x000000', rewardTokenPrice: 12.12, isActive: true, label: 'WMATIC / ETH', protocol: 'sushiswap', ... – DBWeinstein Jun 02 '21 at 02:58
  • this yields "undefined". `console.log(theparsedJSONdata[walletAddress])` – DBWeinstein Jun 02 '21 at 03:04
  • Inside `pullAndParseAPISushi`, should this be how the response is parsed? `var theparsedJSONdata = JSON.parse(response.getContentText());` – Nick Jun 02 '21 at 03:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/233211/discussion-between-dbweinstein-and-nick). – DBWeinstein Jun 02 '21 at 03:08