Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS SyntaxError: Unexpected token in JSON at position 0

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.

like image 892
now_world Avatar asked May 02 '19 22:05

now_world


People also ask

How do I fix unexpected token a JSON at position 0?

The "Unexpected token u in JSON at position 0" error occurs when we pass an undefined value to the JSON. parse or $. parseJSON methods. To solve the error, inspect the value you're trying to parse and make sure it's a valid JSON string before parsing it.

What is unexpected token U in JSON at position 0?

The "Unexpected token u in JSON at position 0" is a typically Javascript error that will show up in your console log if the client has been directed to execute JSON. parse() on a string beginning with u instead of a stringified object. "u" is typically a stringified version of the undefined primitive.

What does unexpected token in JSON at position 0 mean in Quickbooks?

“Unexpected token < in JSON at position 0” is one of the fault which users have perceived in the bulk at the time of operations as similar to as a software developer. Quite often which happens in a situation while fetch function is used for sending an API request from a client.

What does position mean in JSON?

The position in the error message indicates which byte within the file the error occurs around.


2 Answers

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,"")
}
// OR (faster),
let removeByteOrderMark = a=>a[0]=="\ufeff"?a.slice(1):a
like image 147
randomuser5215 Avatar answered Oct 06 '22 23:10

randomuser5215


We had the same issue with JSON.parse() in a project we're working on. We're just using JSON.stringfy() and the object was working fine, but on the other platform where we received the data it had a similar error "Unexpected token in JSON at position 1".

Here's how we made it work:

  1. In NodeJs we encoded the stringified object using encodeURI() then sent it
  2. On the client side: decoded the string then used JSON.parse() on the decoded string

Analysis:

I tried to print the characters from the position, then replaced it with an empty string, then we realized it prints other weird characters even after replacing them. After that I realized it's an HTML code "&quot;" so instead of replacing it we went for encoding the string it and decoding.
We tried it on our case and it's working no problems

like image 20
Mostafa Avatar answered Oct 06 '22 23:10

Mostafa