I encountered a weird behaviour of JSON.parse method in javscript.
If you pass it a string in quotes like - "\"I am a random string\""
Instead of throwing an error it will parse the string and return the same
var a = '"I am a random string"';
var b = JSON.parse(a); // no error, parsing is successful
console.log(b); // output "I am a random string"
I am wondering what could be the cause of this? Is a string in quotes considered a valid JSON object?
"foo bar"is how strings are encoded in JSON. There doesn't have to be an object or array at the top level. Any value is fine.JSON.parse('null'),JSON.parse('true'), etc are all valid too.acontains a string, which, if interpreted in JSON format, is a valid string. Now if you tryvar a = "I am a random string"; var b = JSON.parse(a);, you'll get an error.