15

Hi I am getting this error using angularjs through the chrome console:

SyntaxError: Unexpected token N
    at Object.parse (native)
    at fromJson (http://localhost:3000/assets/angular.js?body=1:803:14)
    at $HttpProvider.defaults.defaults.transformResponse    
http://localhost:3000/assets/angular.js?body=1:9471:18)
    at http://localhost:3000/assets/angular.js?body=1:9446:12
    at Array.forEach (native)
    at forEach (http://localhost:3000/assets/angular.js?body=1:149:11)
    at transformData (http://localhost:3000/assets/angular.js?body=1:9445:3)
    at transformResponse (http://localhost:3000/assets/angular.js?body=1:10061:17)
    at wrappedCallback (http://localhost:3000/assets/angular.js?body=1:7510:59)
    at http://localhost:3000/assets/angular.js?body=1:7583:26 angular.js?body=1:6350
(anonymous function) angular.js?body=1:6350
(anonymous function) angular.js?body=1:5421
wrappedCallback angular.js?body=1:7512
(anonymous function) angular.js?body=1:7583
Scope.$eval angular.js?body=1:8927
Scope.$digest angular.js?body=1:8790
Scope.$apply angular.js?body=1:9013
done angular.js?body=1:10266
completeRequest angular.js?body=1:10450
xhr.onreadystatechange

I am doing a get() request through angular where the json is:

[{"_id":"51f96144c885552bda000015","company_id":"51f82116c88555bf48000004","description":"ENGINEER FOR BEST COMPANY",
"industry_id":null,"location_city":"Pittsburgh","location_coordinates":[-79.9556424,40.4379259],"location_state":"PA","location_zip":"15213","name":"Engineer "},
{"_id":"51f972a5c885552bda000026","company_id":"51f82116c88555bf48000004","description":"has to do everything","industry_id":null,"location_city":"Pittsburgh","location_coordinates":[-79.9418166,40.4443735],"location_state":"PA","location_zip":"15289","name":"job #2"}]

Does anyone know what this means?

6
  • 1
    How are you generating the JSON? It seems to be malformed. Commented Aug 6, 2013 at 3:32
  • 1
    looks like mongodb json Commented Aug 6, 2013 at 3:38
  • 1
    yes, its mongodb json for my rails app Commented Aug 6, 2013 at 4:20
  • 1
    @Blender: what are the specific problems you see in that JSON? I'm getting the exact same error from somebody else's API, which I'm consuming. I need to guide them to how to fix their output... Thanks! Commented Oct 31, 2013 at 23:20
  • 1
    @Josh David: If you found a solution, please share. (Be sure to provide the solution as the answer to your own question, so folks can upvote it.) Commented Oct 31, 2013 at 23:23

4 Answers 4

32

Any SyntaxError: Unexpected token means you've got some malformed JSON, which is usually a string in there that's not wrapped in quotes. Only the following are supported data-types in JSON:

  • string (any text wrapped in quotes)
  • array (an 'array literal', in [])
  • object (an 'object literal', in {})
  • boolean (true or false, not wrapped in quotes)
  • integers or numbers (not wrapped in quotes)
  • null (not wrapped in quotes)

Specifically, SyntaxError: Unexpected token N is often the result of accidentally returning a NaN in your JSON, although it could simply be some other unwrapped string. NaN is not a supported value in JSON, nor is any other text that isn't wrapped in quotes, except true, false and null (and numbers). So, although you do indeed have two nulls in your JSON sample, it shouldn't be the problem. (Your 'N' in the error isn't lowercase, as it would be in null.)

The presence of that capital letter 'N' in the error makes me suspect that you were accidentally returning a NaN somewhere in your JSON output, even though it's not present in your sample. The only other capital-N's you have in your sample are safely wrapped in quotes, and are in the middle of the string in any case.

The key is to ensure that you're properly sanitizing your outputs on the server, and substituting a zero for any NaN values, or wrapping in quotes, etc. Alternately, you could try to deal with such errors on the client, but it's much easier at the point of origin, so you can use conditional logic where the content is generated, rather than needing to use a 'dirty JSON parser'. (It's kind of like the difference between clearing your floor before bed while the lights are still on, and you know where everything is, rather than needing to detect and avoid unknown obstacles in the dark.)

Sign up to request clarification or add additional context in comments.

Comments

5

In my case, the json string , keys (name, age, motto.) are not being decorated by double quote when using JSON.parse() method.

informal :

{
    name: "Jhon Brown",
    age: 30,
    motto: "Please, choose good."
}

will produce error like: SyntaxError: Unexpected token n at Object.parse (native)

formal:

{
    "name": "Jhon Brwon",
    "age": 30,
    "motto": "Please, choose good."
}

Comments

1

It could also be the result of

Notice: Undefined index: project_id in /var/www/html/

generated by php.

1 Comment

Excellent point. Or, more generally, we can say that it can be caused by any uncaught error being piped directly into the output without sanitization.
1

I had the same problem, with another letter instead of N. After a few tests, I realized this letter was in fact the first letter of the string (which I thought was converted into JSON) that I was sending ! (in my case H for "Hello World").

The problem was that the JSON I sent was not valid. (A JSON is just a string with a norm associated to it that enables it to be recognized across different interfaces) In fact, I was just sending a String which was not serialized (put in the right format) with the JSON norm. Which I think is also the case for you. So, the bug comes from your back-end.The fix depends on which backend you are using. I can help you if it's java !

You can try sending valid JSON replacing the data you send by {"id":1,"name":"test"} which is a valid JSON. Your error should disappear on this case and this proves your JSON serialization is not right.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.