Help Concantenating JSON Keys Before Pushing to New Array
I'm working with some copied javascript in Google Apps Script that retrieves data from the Sleeper Fantasy Football API. I don't know much beyond toying around with small bits of javascipt for these types of side-projects, so the help is much appreciated.
Towards the middle, you'll see a new array is being created using some values pulled from JSON. All I want to do is retrieve the corresponding values for "first_name" and "last_name" together, as a concatenated string, prior to pushing them to the new array. Currently, they come through as individual values in the newly created array, and are therefore written to Google Sheets as separate cells.
For Google Sheets formula reasons that I won't bother going through, concatenating afterwards when the data is already in Google Sheets does not easily give me the freedom to do what I'd like to do with the data.
// call to sleeper's Players API -
var PDB = UrlFetchApp.fetch("https://api.sleeper.app/v1/players/nfl");
var json = JSON.parse(PDB);
Logger.log(json[0])
// API returns a nested json w/ each player id as key
// loop through each player to create multi-array
// create empty array
var keys = []
// loop using player ID for each array
for(var key in json){
var arr = [key , json[key]["first_name"],json[key]["last_name"],json[key]["fantasy_positions"],json[key]["team"],json[key]["age"],json[key]["injury_status"],json[key]["status"],json[key]["active"]]
// push results from loop into empty array
keys.push(arr);
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("All");
var headers = ss.getRange('1:2').getValues();
ss.clear();
ss.getRange('1:2').setValues(headers);
ss.getRange(3,2,keys.length,9).setValues(keys);
Posted Sep 24, 2021, 8:34 PM UTC
If I understand correctly and you just want to concatenate first__name and last_name together into a new string:
Thanks! I'll try it in just a bit. Does the fullName constant go inside the for loop and directly above the array variable, or prior to the for loop? If inside, I should be adding a semicolon at the end, correct?
I assume you want a fullName for each player so it would be inside the for loop before you create the array
arrSemi-colons are usually automatically inserted by JavaScript at the end of expressions, however, yes it is a good habit to do it manually. I use prettier which does it for me whenever I save a file, so I'm lazy typing them when I code.
It's very rare that it's absence will cause an issue, but it can happen.
I thought as much. I've tried it, and it's not working, but I'm guessing it's a simple fix that hopefully you can easily identify without taking up much of your time. Rather than spitting out the expected Tom Brady, for example, the array spits out ${json[key][first_name]} ${json[key][last_name]} as the first value. In essence, the code is not being interpreted as anything other than a literal string. Seems like it's not acknowledging any constant exists. I'm pasting the updated code below in case you notice a clear and obvious typo or change that needs to be made.
You need to use backticks (`) not quotation marks (' or ") for variables to be evaluated inside a string, which is what the original poster had.