FetchingURL Using JSON on Google Sheets
I am attempting to use JSON to extract followers on Spotify Playlists. It seems like various playlists have different HTML data - which makes it complicated.
Here's the code I have currently in Google Apps Scripts. (courtesy of @Tanaike)
function SAMPLE(url) {
const res = UrlFetchApp.fetch(url).getContentText();
const v = res.replace(/&/g, "&").match(/Spotify\.Entity \=([\s\S\w]+?);/);
return v && v.length == 2 ? JSON.parse(v[1].trim()).followers.total : "Value cannot be
retrieved.";
}
Then I just used =SAMPLE(the link) in the cells
However, I am getting this error on some playlist links (I don't know why): SyntaxError: Unexpected end of JSON input (line 4).
Examples of working links: https://open.spotify.com/playlist/5aSO2lT7sVPKut6F9L6IAc https://open.spotify.com/playlist/7qvQVDnLe4asawpZqYhKMQ
Examples of nonworking links (shows the error stated above) https://open.spotify.com/playlist/2Um96ZbAH1fFTHmAcpO50n https://open.spotify.com/playlist/68jtRFcWtaFNHKO5wYLBsk
About 80% of the links work (I have over 400). Any guidance or help would be greatly appreciated.
Thank you everyone!
I believe your goal as follows.
-
You want to retrieve the number of followers from the following URLs.
https://open.spotify.com/playlist/5aSO2lT7sVPKut6F9L6IAc https://open.spotify.com/playlist/7qvQVDnLe4asawpZqYhKMQ https://open.spotify.com/playlist/2Um96ZbAH1fFTHmAcpO50n https://open.spotify.com/playlist/68jtRFcWtaFNHKO5wYLBsk https://open.spotify.com/playlist/2phIYXF2hAd5gTj00IwXbU https://open.spotify.com/playlist/7MBKSFzpPLQ9ryPtydXVwf https://open.spotify.com/playlist/148My4xA7WoEaNDmnl2A8Z https://open.spotify.com/playlist/4zMaYNXz2pP1OPGz9SIJhX https://open.spotify.com/playlist/2hBohCkXzjzTLh6jtd7gUZ
Modification points:
- I think that in the current stage, all JSON data is retrieved and the data is parsed, and then, the value is retrieved. In this case, I thought that the specific characters might be included and those characters might occur the issue.
So in this answer, I would like to retrieve the part of value from JSON data and parsed it and returned value. The modified script is as follows.
Modified script:
function SAMPLE(url) {
const res = UrlFetchApp.fetch(url).getContentText();
const v = res.match(/followers":({[\s\S\w]+?})/);
return v && v.length == 2 ? JSON.parse(v[1].trim()).total : "Value cannot be retrieved.";
}
Result:
When above script is used for the above 9 URLs, the following result is obtained.
Note:
- This sample script is for the URLs in your question. So when you tested it for other URLs, the script might not be able to used. And, when the structure of HTML is changed at the server side, the script might not be able to used. So please be careful this.
Updated on January 15, 2022:
It seems that the specification of the HTML data is a bit changed. So I updated the above script as follows.
Sample script:
function SAMPLE(url) {
const res = UrlFetchApp.fetch(url).getContentText();
const v = res.match(/"followers":({[\s\S\w]+?})/);
return v && v.length == 2 ? JSON.parse(v[1].trim()).total : "Value cannot be retrieved.";
}