-2

Update: Fixed mistakes in example code. Turned out my problem was caused by an additional 'records' in var jsonVar = jsonVar.concat(results.records);

How can I concat JSON objects in a loop? I can concat 2 JSON objects 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);

To add a 3rd JSON object I know I can just add .concat(json3.records) but how can I dynamically concatenate JSON objects in a loop?

Example: Say values.length = 5, this means 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 works now (had a typo here)
var jsonVar = jsonVar.concat(results.records); 

}
dean2020
  • 645
  • 2
  • 8
  • 25
  • 1
    What (do you think) a "JSON object" is? – Amit Apr 23 '16 at 08:57
  • it looks more like an async problem, than a concat problem. – Nina Scholz Apr 23 '16 at 08:57
  • maybe you have a look here: http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron – Nina Scholz Apr 23 '16 at 09:07
  • Sorry guys, I overlooked an error in my code. I had an extra 'records' in `var jsonVar = jsonVar.records.concat(results.records);` This should be `var jsonVar = jsonVar.concat(results.records);` – dean2020 Apr 23 '16 at 09:56
  • @dean2020 Don't put corrections in comments, edit the question and fix it. There seems to be another typo: `results = UrlFetchApp.fetch(url)` should probably be `response = ...` – Barmar Apr 23 '16 at 10:03
  • Is `UrlFetchApp` an async function? Does it take a callback function? – Barmar Apr 23 '16 at 10:03
  • Thanks for the pointers Barmar. async or not, my code seems to be working fine. Some discussion on 'UrlFetchApp` async/callback http://stackoverflow.com/questions/31241396/is-google-apps-script-synchronous – dean2020 Apr 23 '16 at 10:29

2 Answers2

0

You could do something like:

var jsonVar = [];
for (var i=0; i<values.length ; i++) {

  results= UrlFetchApp.fetch(url);
  Utilities.sleep(1000);
  var results = JSON.parse(response);

  jsonVar = jsonVar.concat(results.records); 

}

But I am not sure if this would work because UrlFetchApp.fetch() seems to be asynchronous. This means the response is not guaranteed to be initialized to correct value if it takes more than 1000ms

Kashif
  • 1,238
  • 10
  • 15
-1

A JSON object is a Javascript object. You can dynamically set the object name

  for (var i=0; i<values.length ; i++) {
  json[i].records.concat(json[i + 1].records)
  }
noideawhattodo
  • 176
  • 2
  • 12