Reputation: 785
I'm using Google Apps Scripts to parse some JSON and it's not working...
Here's my pseudo-code:
function fetchInsta() {
var url = "xxx";
Logger.log(url);
var result = UrlFetchApp.fetch(url);
var json = JSON.parse(result.getContentText());
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var existingids = sheet.getRange("D:D");
for(i in json.data){
for(j in existingids){
if(json.data[i].id = existingids[i]){
var match = true;
}
}
if(match != true){
var id = json.data[i].id;
var image = json.data[i].images.standard_resolution.url;
var username = json.data[i].user.username;
var link = json.data[i].link;
var date = json.data[i].createdtime;
sheet.appendRow([image,username,link,id,date]);
Logger.log([image,username,link,id,date]);
}
}
}
I've removed the URL as it has my API key in it. But here's a sample of the JSON returned:
"data":[
{
"attribution":null,
"tags":[ ],
"type":"image",
"location":{ },
"comments":{ },
"filter":"Normal",
"created_time":"1430751422",
"link":"https:\/\/instagram.com\/p\/2Q6AiIG4hT\/",
"likes":{ },
"images":{ },
"users_in_photo":[ ],
"caption":{ },
"user_has_liked":false,
"id":"977536242480285779_10266761",
"user":{ }
},
I'm getting the variables image, username, link fine - but id and date are both undefined. Any ideas what's happening? What am I doing wrong?
Upvotes: 0
Views: 198
Reputation: 31300
This line of code:
if(json.data[i].id = existingids[i]){
Is using an assignment operator to test for equality. Equality tests are done with double or triple equal signs. If in doubt, use triple equal signs.
Should be:
if(json.data[i].id === existingids[i]){
Upvotes: 2