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

010
Harlequin-91•

If I understand correctly and you just want to concatenate first__name and last_name together into a new string:

const fullName = `${json[key][first_name]} ${json[key][last_name]}`    // Use interpolation to create full name 
var arr = [key , fullName, json[key]["fantasy_positions"], ...] // The rest
3
dcburnquistOP•

const fullName = `${json[key][first_name]} ${json[key][last_name]}` // Use interpolation to create full nam

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?

2
Harlequin-91•

I assume you want a fullName for each player so it would be inside the for loop before you create the array arr

Semi-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.

2
dcburnquistOP•

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.

     for(var key in json){
 const fullName = '${json[key][first_name]} ${json[key][last_name]}';

 var arr = [key , fullName, json[key]["fantasy_positions"],json[key]["team"],json[key]["age"],json[key]["injury_status"],json[key]["status"],json[key]["active"]]   

 keys.push(arr); 
                      }
2
Anbaraen•

You need to use backticks (`) not quotation marks (' or ") for variables to be evaluated inside a string, which is what the original poster had.

3
Greyjungle27•

Stop using var, my eyes can’t take it. “let” is significantly more readable, accepted and modern. Using var is like punching a hole through the roof of your car and climbing in rather than using the door.

1
dcburnquistOP•

Thanks for the feedback, but as stated in my very first paragraph, I don't code. This is copied and pasted from Reddit a while back for a fantasy football hobby project. It was just meant to be functional.

1