I have been working with JSON for quite a while now.
I know that JSON is stringified
JavaScript Object.
I also know what makes JSON, as discussed in the below thread.
What is the difference between JSON and Object Literal Notation?
Question: But, I want to know if there is typeof
kind of way that would tell me if a given string is JSON or just any other string.
What I have observed so far:
1. var data = {"name":"JS"};
alert(typeof(data)); // object. Agree!
2. // I know this is JSON, by looking at the syntax
var data = '{"name":"JS"}';
alert(typeof(data)); // string.
3. // I know this is just any other string (NOT JSON), by looking at the syntax.
var data = "AnyOtherString";
alert(typeof(data)); // string
Is there any way,in JavaScript, that I could differentiate between Point 2 & Point 3 above. Possibly, something similar to typeof
that would tell me if its just a string or a JSON(also a string).?