GVelascoh GVelascoh - 2 months ago 9
JSON Question

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": "*NETEVENT*","sensors": "0B","ti": 1047215800,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 1047215800,"to": 0},
{"type": 30,"tag": " __START__","sensors": "00","ti": 1047217510,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 1047217511,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 1047217511,"to": 0},
{"type": 30,"tag": " __START__","sensors": "00","ti": 1047217956,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 1047217957,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 1047217957,"to": 0},
{"type": 30,"tag": " __START__","sensors": "00","ti": 1047218051,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0A","ti": 1047218051,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 1047218052,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 1047218052,"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": 0000001564,"to": 0},
{"type": 30,"tag": " __START__","sensors": "00","ti": 1047159660,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0A","ti": 1047159660,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 1047159661,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 1047159662,"to": 0},
{"type": 30,"tag": " __START__","sensors": "00","ti": 1047160593,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 1047160593,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 1047160594,"to": 0},
{"type": 30,"tag": " __START__","sensors": "00","ti": 1047161631,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0A","ti": 1047161631,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 1047161632,"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?

Answer

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.