0

I am using Meteors http to search for a track using the Spotify API. My problem is that when the server gets the Json file there is new line characters in the file so I can not parse it. I found a solution to escape the new line characters but I have a suspicion that there has to be a better way.

My HTTP call

function artFromSpotify(title, artist, album){
  var qString = 'artist:' + artist + ' track:' + title;
  var result = HTTP.call("GET", 'https://api.spotify.com/v1/search?'+qString, {
    params: {'q': qString,
      'type' : 'track',
      'limit' :'1'}
    });   

Spotify JSON Response

content: '{\n  "tracks" : {\n    "href" : "https://api.spotify.com/v1/search?query=artist%3AElton+John+track%3ARocket+Man&offset=0&limit=1&type=track",\n    "items" : [ {\n      "album" : {\n        "album_type" : "album",\n        "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "IE", "IS", "IT", "LI", "LT", "LU", "LV", "MC", "MT", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "RO", "SE", "SG", "SI", "SK", "SV", "TR", "TW", "UY" ],\n        "external_urls" : {\n          "spotify" : "https://open.spotify.com/album/46g6b33tbttcPtzbwzBoG6"\n        },\n        "href" : "https://api.spotify.com/v1/albums/46g6b33tbttcPtzbwzBoG6",\n        "id" : "46g6b33tbttcPtzbwzBoG6",\n        "images" : [ {\n          "height" : 634,\n          "url" : "https://i.scdn.co/image/07039e8a1fcd1fda967a021e8c4d5ccaf5dbaa8d",\n          "width" : 640\n        }, {\n          "height" : 297,\n          "url" : "https://i.scdn.co/image/e62555a039122c4c26ea7fb738e705f1de1795d2",\n          "width" : 300\n        }, {\n          "height" : 63,\n          "url" : "https://i.scdn.co/image/c3581519b056a17b9056fa579536e9788868464d",\n          "width" : 64\n        } ],\n        "name" : "Honky Chateau (Remastered)",\n        "type" : "album",\n        "uri" : "spotify:album:46g6b33tbttcPtzbwzBoG6"\n      },\n      "artists" : [ {\n        "external_urls" : {\n          "spotify" : "https://open.spotify.com/artist/3PhoLpVuITZKcymswpck5b"\n        },\n        "href" : "https://api.spotify.com/v1/artists/3PhoLpVuITZKcymswpck5b",\n        "id" : "3PhoLpVuITZKcymswpck5b",\n        "name" : "Elton John",\n        "type" : "artist",\n        "uri" : "spotify:artist:3PhoLpVuITZKcymswpck5b"\n      } ],\n      "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "IE", "IS", "IT", "LI", "LT", "LU", "LV", "MC", "MT", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "RO", "SE", "SG", "SI", "SK", "SV", "TR", "TW", "UY" ],\n      "disc_number" : 1,\n      "duration_ms" : 281613,\n      "explicit" : false,\n      "external_ids" : {\n        "isrc" : "GBAMB7200006"\n      },\n      "external_urls" : {\n        "spotify" : "https://open.spotify.com/track/2zvot9pY2FNl1E94kc4K8M"\n      },\n      "href" : "https://api.spotify.com/v1/tracks/2zvot9pY2FNl1E94kc4K8M",\n      "id" : "2zvot9pY2FNl1E94kc4K8M",\n      "name" : "Rocket Man (I Think It\'s Going To Be A Long Long Time)",\n      "popularity" : 65,\n      "preview_url" : "https://p.scdn.co/mp3-preview/e0f814581fb0c9f5faeb02146aa4c1280510b4f3",\n      "track_number" : 5,\n      "type" : "track",\n      "uri" : "spotify:track:2zvot9pY2FNl1E94kc4K8M"\n    } ],\n    "limit" : 1,\n    "next" : "https://api.spotify.com/v1/search?query=artist%3AElton+John+track%3ARocket+Man&offset=1&limit=1&type=track",\n    "offset" : 0,\n    "previous" : null,\n    "total" : 54\n  }\n}'
Cheech
  • 355
  • 3
  • 16
  • 1
    Why are you having trouble parsing it? Newlines are perfectly valid in JSON (as long as they're not inside a string value, in which case they do indeed need to be escaped). – Spudley Oct 22 '15 at 20:21
  • You should have no trouble parsing it, but if you don't want to deal with the newlines, you can use a package such as [xinranxiao:spotify-web-api](https://atmospherejs.com/xinranxiao/spotify-web-api) to make the calls for you – Xinzz Oct 22 '15 at 20:34
  • You guys are correct /n is not the problem. I was not using dot notation correctly. – Cheech Oct 23 '15 at 21:54

1 Answers1

2
query = 'https://api.spotify.com/v1/search?query=artist%3AElton+John+track%3ARocket+Man&offset=0&limit=1&type=track'

HTTP.get(query, function(err, res){
  result = JSON.stringify(
    JSON.parse(res.content).tracks
  )
})

result will now contain the json string without newlines.

However for most purposes you will actually want the result from JSON.parse(res.content) instead.

HTTP.get(query, function(err, res){
  result = JSON.parse(res.content).tracks
})

Now result is an object you can work with.

JeremyK
  • 3,240
  • 1
  • 11
  • 24