0

I'm using a Blockspring API that returns a JSON array of objects (read in from a Google Sheet). However, whenever I try to access an object from the array, an "undefined" value is returned. I have attached the code and the console log below. Does anyone have any ideas why?

blockspring.runParsed("query-public-google-spreadsheet", { "query": "SELECT A, B, C", "url": 
    "https://docs.google.com/spreadsheets/d/1ZYvcYf_41aghdXRSpg25TKW4Qj9p1Lpz92b1xG-9R1Q/edit?usp=sharing"}, 
    { "api_key": "br_50064_1fe91fe1478ef990dc8b5e9b4041c2c476670306" }, function(res){  
        var obj=res.params;
        console.log(obj);
        var temp=obj[0];
        console.log(temp);
    }

Console Output

Justin Borromeo
  • 1,201
  • 3
  • 13
  • 26
  • See *"I'm trying to access a property but I get only `undefined` back?"* in the accepted answer of the duplicate. – Felix Kling Jan 08 '17 at 03:32

2 Answers2

0

You need to use obj.data[0] to access the first element of an array.

Looking at your output in the console, it seems you are missing the data property of obj.

Object obj doesn't have a property with name 0 so it returns undefined.

Abhinav Galodha
  • 9,293
  • 2
  • 31
  • 41
0

I would need to play around with it myself but I can tell that the problem is just how you are accessing the info.

When you try to grab the data with var temp=obj[0] you are treating object like an array when is it not. I would recommend trying to grab the data using this:

//get the actual array
JSONArray theArray = obj.getJSONArray("data"); //I believe it is stored in an array called data... could be that the obj is just fine
// now get the first element:
JSONObject firstItem = theArray.getJSONObject(0);
// and so on
String name = firstItem.getString("Name"); // A

You most likely can grab it using var temp = obj.data[0];