The body of the response from Authorize.net's sandbox API is:

"{"messages":{"resultCode":"Error","message":[{"code":"E00012","text":"You have submitted a duplicate of Subscription 5777085. A duplicate subscription will not be created."}]}}"

but when I go to parse it:

try {
   bodyObj = JSON.parse(body);
} catch (ex) {
   console.error(ex);
}

I get this error: SyntaxError: Unexpected token  in JSON at position 0

And this: console.log(response.headers['content-type']);

returns this: application/json; charset=utf-8

What am I doing wrong? I want to parse the JSON into a JS object.

1 Answers

2
smileycreations15 On Best Solutions

Actually you didn't see it, but there was a invisible unicode character, specifically the byte order mark at the beginning of the JSON.
Since the byte order mark is not a valid JSON character, JSON.parse rejected it.
byte order mark image
To remove, use the following code.

function removeByteOrderMark(str){
    return str.replace(/^\ufeff/g,"")
}