Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON syntax error: 'unexpected number' or 'JSON.parse: expected ',' or '}' after property value in object'

I receive this response from a POST request using $.ajax():

{"command": 6,"log_size":50,"log":[     {"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 1047161877,"to": 0},     {"type": 30,"tag": " __START__","sensors": "00","ti": 0000011410,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "0A","ti": 0000011411,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 0000011411,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 0000011412,"to": 0},     {"type": 30,"tag": " __START__","sensors": "00","ti": 1047215799,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "0A","ti": 1047215799,"to": 0},     {"type": 30,"tag": " __START__","sensors": "00","ti": 1047218051,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 0000002598,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 1047068795,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 1047068796,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "0A","ti": 1047071223,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 1047071224,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 1047071225,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "0A","ti": 0000000010,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 0000000012,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "0C","ti": 1047130533,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "0A","ti": 0000000026,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "0A","ti": 0000000180,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 0000000206,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "09","ti": 0000000212,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "08","ti": 0000000383,"to": 0},     {"type": 30,"tag": " __START__","sensors": "00","ti": 0000001562,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "0A","ti": 0000001563,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 0000001564,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 1047161632,"to": 0},     {"type": 30,"tag": " __START__","sensors": "00","ti": 1047161875,"to": 0},     {"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 1047161876,"to": 0} ], "response":"ok"} 

For IE works fine, in Chrome appears "Syntax error: unexpected number" and in Firefox the message is "SyntaxError: JSON.parse: expected ',' or '}' after property value in object"

In various online JSON parsers and validators the format of the response seems to be OK, but in firefox and chrome not works.

Any idea why this happens?

like image 303
gustavovelascoh Avatar asked Mar 08 '13 14:03

gustavovelascoh


People also ask

How do I fix unexpected token in JSON error?

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 JSON parse error?

The "SyntaxError: JSON. parse: unexpected character" error occurs when passing a value that is not a valid JSON string to the JSON. parse method, e.g. a native JavaScript object. To solve the error, make sure to only pass valid JSON strings to the JSON.

What error does JSON parse () throw when the string to parse is not valid JSON?

Exceptions. Throws a SyntaxError exception if the string to parse is not valid JSON.

What is JSON parse error unexpected EOF?

Unexpected EOF (end of file) means that there might be something like a missing or duplicate '}' or ',' character. Check in the tone studio files for any . json file extensions and try to open them in a code editor like vscode for example.


1 Answers

A number can't start with a not significative 0.

This is invalid : "ti": 0000011410

From JSON.org :

enter image description here

You should fix it at the source but if you can't, assuming your JSON is always similar to this one (no numbers in strings), then you might probably fix it with a regex :

var obj = JSON.parse(str.replace(/ 0+(?![\. }])/g, ' ')); 

You can't even here use the evil eval because "0000011410" would be parsed as a octal :

console.log(eval('({"ti": 0000011410})')); 

outputs

{ti: 4872} 

This probably explains why it was considered safer to forbid numbers starting with non significative 0 in JSON.

like image 80
Denys Séguret Avatar answered Oct 01 '22 04:10

Denys Séguret