2
votes

I want to send back properly formatted JSON from a node server to the front end.

var express = require('express');
var app = express();
app.get('/route', function(req, res){
    res.json(tracks)
});

Then in the front end, I am making a GET request using jQuery

$.get('http://localhost:8888/route', function(data){
    console.log(JSON.parse(data));
});

This is returning the data, however, it is returning it as a string. I have tried to use JSON.parse but am getting the following error message. data on the node side is an array of objects. Perhaps this is causing an issue.

Uncaught SyntaxError: Unexpected token , in JSON at position 2847(…)

To fill the tracks array I have the following code

tracks = [];
for (var i = 0; i < songLinks.length; i++) {
  request('https://api.spotify.com/v1/tracks/' + songLinks[i].split(":").pop(), function(error, response, body) {
    tracks.push(body)
  });
}

The first item in the array of tracks is:

"{↵ "album" : {↵ "album_type" : "album",↵ "artists" : [ {↵
"external_urls" : {↵ "spotify" : "https://open.spotify.com/artist/1yAwtBaoHLEDWAnWR87hBT"↵ },↵
"href" : "https://api.spotify.com/v1/artists/1yAwtBaoHLEDWAnWR87hBT",↵ "id" : "1yAwtBaoHLEDWAnWR87hBT",↵ "name" : "Modest Mouse",↵
"type" : "artist",↵ "uri" : "spotify:artist:1yAwtBaoHLEDWAnWR87hBT"↵ } ],↵
"available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IS", "IT", "JP", "LI", "LT", "LU", "LV", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "SE", "SG", "SK", "SV", "TR", "TW", "US", "UY" ],↵
"external_urls" : {↵ "spotify" : "https://open.spotify.com/album/4YvdAtWh6LlnIrv0qIqkCF"↵ },↵
"href" : "https://api.spotify.com/v1/albums/4YvdAtWh6LlnIrv0qIqkCF",↵ "id" : "4YvdAtWh6LlnIrv0qIqkCF",↵ "images" : [ {↵ "height" : 640,↵ "url" : "https://i.scdn.co/image/411d254a0e46f509dda22f58d699782f16f7bd44",↵
"width" : 640↵ }, {↵ "height" : 300,↵ "url" : "https://i.scdn.co/image/982b3b7c3ad3e81aed9e0475da07894262f93923",↵
"width" : 300↵ }, {↵ "height" : 64,↵ "url" : "https://i.scdn.co/image/e4a25681c1163b349ff71b464d2cfc8fda089d58",↵
"width" : 64↵ } ],↵ "name" : "No One's First, And You're Next",↵ "type" : "album",↵ "uri" : "spotify:album:4YvdAtWh6LlnIrv0qIqkCF"↵ },↵ "artists" : [ {↵ "external_urls" : {↵ "spotify" : "https://open.spotify.com/artist/1yAwtBaoHLEDWAnWR87hBT"↵ },↵
"href" : "https://api.spotify.com/v1/artists/1yAwtBaoHLEDWAnWR87hBT",↵ "id" : "1yAwtBaoHLEDWAnWR87hBT",↵ "name" : "Modest Mouse",↵
"type" : "artist",↵ "uri" : "spotify:artist:1yAwtBaoHLEDWAnWR87hBT"↵ } ],↵ "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IS", "IT", "JP", "LI", "LT", "LU", "LV", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "SE", "SG", "SK", "SV", "TR", "TW", "US", "UY" ],↵ "disc_number" : 1,↵ "duration_ms" : 330573,↵ "explicit" : false,↵ "external_ids" : {↵ "isrc" : "USSM10700655"↵ },↵ "external_urls" : {↵ "spotify" : "https://open.spotify.com/track/6Z3pIqEp5n5faSopwto0tB"↵ },↵ "href" : "https://api.spotify.com/v1/tracks/6Z3pIqEp5n5faSopwto0tB",↵ "id" : "6Z3pIqEp5n5faSopwto0tB",↵ "name" : "King Rat",↵ "popularity" : 38,↵ "preview_url" : "https://p.scdn.co/mp3-preview/037294fdd97c94bebf14e7ec1b1fb2c0a1986adc",↵ "track_number" : 7,↵ "type" : "track",↵ "uri" : "spotify:track:6Z3pIqEp5n5faSopwto0tB"↵}"

Below is a screenshot of the response also in case it will make my question any clearer.

enter image description here

1
It means data is not a valid json - Rayon
@Rayon thanks! am i doing something wrong on the node side? - user6002037
Could you post the code how you create the data on the node.js side? Could you also post one json string(index 0 in the screenshot)? - Martin Gottweis
@phantom You can validate your json here: jsonlint.com - Michelangelo
Suggest you just log the data received (rather than the result of the parse): this will allow you to copy it into other tools (or examine it manually) - Richard

1 Answers

1
votes

Change the body to response

request('https://api.spotify.com/v1/tracks/' + songLinks[i].split(":").pop(), function(error, response, body) {
    tracks.push(JSON.parse(body));
  });