"Unexpected token in JSON at position 0" using JSON.parse in Node with valid JSON "Unexpected token in JSON at position 0" using JSON.parse in Node with valid JSON google-chrome google-chrome

"Unexpected token in JSON at position 0" using JSON.parse in Node with valid JSON


It appears api.511.org is applying gzip to any api calls that supply a valid api_key. Also it's returning an invalid first character in the json response.

Here is a workaround:

var request = require('request');var apiUrl = 'http://api.511.org/transit/vehiclemonitoring?api_key=${API_KEYS.API_KEY_511}&format=json&agency=sf-muni';//apiUrl = 'http://ip.jsontest.com/';var response = request({    method: 'GET',    uri: apiUrl,    gzip: true}, function(error, response, body) {    //* workaround for issue with this particular apiUrl    var firstChar = body.substring(0, 1);    var firstCharCode = body.charCodeAt(0);    if (firstCharCode == 65279) {        console.log('First character "' + firstChar + '" (character code: ' + firstCharCode + ') is invalid so removing it.');        body = body.substring(1);    }    //*/    var parsedJson = JSON.parse(body);    console.log('parsedJson: ', parsedJson);});