I'll try to clarify what I mean as quickly as possible.
JSON.parse("te")
VM297:1 Uncaught SyntaxError: Unexpected token e in JSON at position 1
at JSON.parse (<anonymous>)
at <anonymous>:1:6
JSON.parse("ce")
VM342:1 Uncaught SyntaxError: Unexpected token c in JSON at position 0
at JSON.parse (<anonymous>)
at <anonymous>:1:6
As you can see, the parsing fails at position 0 for the string "ce" and at position 1 for the string "te". This means that the parser believes that there is some legal JSON that starts with the character "t". Does anybody know what that would be? Or why the parser fails a character later for t?
Null values and empty strings (“”) are valid JSON values, but the state of being undefined is not valid within JSON. JSON. parse() will fail if it encounters an undefined value.
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.
Exceptions. Throws a SyntaxError exception if the string to parse is not valid JSON.
parse needs to get a string which consists only of unicode characters (see Json parsing with unicode characters). For you the JSON. parse method fails, because your string contains non-unicode characters.
The keyword true
starts with "t". Thus until the parser sees the "e", it doesn't know the syntax is invalid.
The error is somewhat fascinating because it reports "e" as being a token, which is not the way I'd implement a JSON parser. That seems to be a Node/V8 thing, as Firefox rejects the whole token starting from position 1 (the "t").
You can double-check this answer by trying JSON.parse("nulp")
; Node errors on the "p".
The full JSON syntax is as follows:
JSON = null
or true or false
or JSONNumber
or JSONString
or JSONObject
or JSONArray
so the compiler will deal with t
, n
, f
as a valid start for a JSON string.
for more information check: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With