Update: I have corrected the errors in the sample code provided. It turns out that the issue was caused by an extra 'records' in
var jsonVar = jsonVar.concat(results.records);
Is there a way to concatenate multiple JSON objects within a loop? Concatenating two JSON objects can be done like this:
var json1 = {
"records": [{
"id": 28100988,
"work_text_reviews_count": 13,
"average_rating": "3.10"
}, {
"id": 10280687,
"work_text_reviews_count": 80,
"average_rating": "3.87"
}]
}
var json2 = {
"records": [{
"id": 16135639,
"work_text_reviews_count": 0,
"average_rating": "0.00"
}, {
"id": 17978337,
"work_text_reviews_count": 2414,
"average_rating": "3.76"
}, {
"id": 360721218,
"work_text_reviews_count": 4924,
"average_rating": "3.98"
}]
}
var json3 = json1.records.concat(json2.records);
If I wanted to add a third JSON object, I could simply include .concat(json3.records) but how do I go about concatenating JSON objects dynamically in a loop?
For example: Assume values.length = 5, it means that 5 JSON objects need to be concatenated.
for (var i=0; i<values.length ; i++) {
response= UrlFetchApp.fetch(url);
Utilities.sleep(1000);
var results = JSON.parse(response);
// This is now functioning correctly (I had made a mistake here)
var jsonVar = jsonVar.concat(results.records);
}