Unexpected map result in apps script

Viewed 88

I'm working with apps script and have:

var json = UrlFetchApp.fetch(url);
var objArray = JSON.parse(json) 
Logger.log(objArray);    
const indices = objArray.map(({ ' #':id }) => id);
Logger.log(indices);

Logger.log(objArray) gives:

[{ #=4626,  E=Yes,  Assd $=,  Eager=High,  New $=,  State=Louisiana,  Date=06/15/20,  County=St. Martin Parish}, { #=4623,  State=South Carolina,   E=Yes,  Acres=1.50,  Eager=Low,   Date=06/15/20,  New $=}, { County=Sedgwick County,  Eager=High,  Acres=0.13,  New $=,  E=No, #=4620,  Date=06/15/20,  State=Kansas}, ....

Logger.log(indices) gives:

[null, null, ....... null]

However the following:

const values = [
 { " #": "4596", " E": "Yes"},
 { " #": "4597", " E": "No"},
 { " #": "4598", " E": "Maybe"},

]

 const indices = values.map(({ " #" : id }) => id);

 console.log(indices);

when run in apps script produces:

[4596,4597,4598]

Why does the first not work ?

edit:

json:

 [{" #":"4626"," E":"Yes"," Date":"06/15/20"," County":"St. Martin Parish"," State":"Louisiana"," Acres":"15.00","  Eager":"High"},{" #":"4623"," E":"Yes"," Date":"06/15/20"," County":"Greenville County"," State":"South Carolina"," Acres":"1.50","Eager":"Low"},
1 Answers

How about this answer?

Issue and workaround:

From the URL of your replying, I could understand about the reason of your issue. When the values are retrieved from the URL of https://ssjbhokibi.execute-api.us-east-1.amazonaws.com/dev/hello/get, I could confirm the values like your sample input values, and also confirm the same situation. When I checked the character, I could confirm that   of  # is not the space of 0020, it's 00A0 which is the no-break space. I think that this is the reason of your issue.

And also, the value retrieved by UrlFetchApp.fetch(url) is the object of Class HTTPResponse. So in this modification, getContentText() is used for retrieving the text value.

In order to remove your issue, how about the following modification?

Pattern 1:

In this pattern, your script is modified by replcing 0020 to 00A0.

From:

var json = UrlFetchApp.fetch(url);
var objArray = JSON.parse(json) 
const indices = objArray.map(({ ' #':id }) => id);

To:

var json = UrlFetchApp.fetch(url);
var objArray = JSON.parse(json.getContentText());
const indices = objArray.map(({ ' #':id }) => id);
  • When I posted above modified script directly using 00A0 like ' #', it seems that 00A0 is automatically changed to 0020 by the render of Markdown. So in this case, when you directly see the source of my answer, you can see the modified script using 00A0.
  • But in this case, I thought that the readability of the modified script might be low. So I would like to propose one more pattern as follows.

Pattern 2:

In this pattern, the key is created by converting 00A0 with String.fromCharCode(160), and the value is retrieved.

From:

var json = UrlFetchApp.fetch(url);
var objArray = JSON.parse(json) 
const indices = objArray.map(({ ' #':id }) => id);

To:

var json = UrlFetchApp.fetch(url);
var objArray = JSON.parse(json.getContentText());
const indices = objArray.map(({[String.fromCharCode(160) + "#"]: id}) => id);

or

var json = UrlFetchApp.fetch(url);
var objArray = JSON.parse(json.getContentText());
const indices = objArray.map(e => e[String.fromCharCode(160) + "#"]);

Note:

  • In your sample input value, 0020 is used for #. So I couldn't notice about this. But when I updated my answer using 00A0 like ' #', I noticed that 00A0 is automatically changed to 0020. On the other hand, I could confirm that when the source is confirmed by clicking editing button, ' #' uses 00A0. And also, when I confirm your source of question, it can be confirm that the space of [{" #":"4626", uses 00A0. When I tested this for rendering the Markdown, I noticed that it seems that when the render of Markdown is run, 00A0 is changed to 0020 even when that is written in the script. By this, I couldn't notice about the issue from your added sample input value. From this situation, I thought the same questions posted before might also have the same situation with this, and this might be the important point.

References:

Related