According to the JSON spec a string is a legitimate JSON value.
So why does this happen?
You are actually passing the bare word string
in to the the function which of course is not valid JSON. To actually pass in the value "string
" you need to be careful with your JavaScript.
Try this:
JSON.parse("\"string\"")
The extra pair of quotes must be escaped so they become part of the value you pass in to the function.
The Syntax error tells you: "s" is an unexpected token. A string is a valid JSON value but as the spec describes it must be enclosed in double quotes.
string
""
" chars "
Generally, you can use JSON.stringify(myValue)
to check what a properly formatted JSON string of such value would be.
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