1

I'm very bad at coding, so probably this will be the most stupid question possible. I want to find a certain value in a JSON by his position in the array. The problem is that the IDs of the elements aren't sorted but they are 'random' numbers (See the photo). My question is, how can I read a certain value from an element by his position in the array? For example the ID 1001 is in position 0 etc... If I use json['data'][1001][...] i can read separately it but it's difficult to write them using a for cicle.

The JSON is LINK

 function myFunction() {
  
  var response = UrlFetchApp.fetch("http://ddragon.leagueoflegends.com/cdn/"+12.5+".1/data/en_US/item.json");
  var content = response.getContentText();
  var json = JSON.parse(content);
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ITEMS");
  var items = sheet.getActiveRange();
  var itemsPosition = Object.keys(json['data']);
  Logger.log(json['data'][1001]['name']); //works with IDs, I obtain 'Boots'
  Logger.log(json['data'][0]['name']); // doesnt work with position
  Logger.log(json['data'[0]['name']); // doesnt work
}

Photo

sonzo4
  • 11
  • 2

1 Answers1

0

Iterating through obj that came from JSON and retrieving name values

function listallnames() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet name");
  const obj = JSON.parse("Your string");
  let vo = obj.data.map(o => [o.name]);
  sh.getRange(1,1,vo.length,vo[0].length).setValues(vo):
}
Cooper
  • 59,616
  • 6
  • 23
  • 54