I'm using Crockford's json2.js. When I want to stringify, I do JSON.stringify()
...works great.
However, those who've taken a look at the code know that it defers to existing JSON objects and properties. I suspect a certain problem I'm having may be due to this deference.
Is there a property of the JSON object I can check to see if the browser is using Crockford's object, or some other? It would be nice to be able to do something like alert(JSON.version());
To check if JavaScript object is JSON, we can use the JSON. parse method. const isJson = (data) => { try { const testIfJson = JSON. parse(data); if (typeof testIfJson === "object") { return true; } else { return false; } } catch { return false; } };
JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).
jquery json provide several method for getting key value or check if key exists etc. In this example we will use hasOwnProperty method of json object that will help to check if key exists or not in jquery. hasOwnProperty return true if key is exists and return false if key is not exists on given javascript json.
To access the JSON object in JavaScript, parse it with JSON. parse() , and access it via “.” or “[]”.
JSON encoded resource is not an object. It is a string. Only after you decode it or in Javascript JSON.parse () it does the JSON resource become an object. Therefore if you test a resource coming from a server to see if it is JSON, it is best to check first for String, then if is a not a <empty string> and then after parsing if it is an object.
Object Syntax. Example. { "name":"John", "age":30, "car":null } JSON objects are surrounded by curly braces {}. JSON objects are written in key/value pairs. Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null). Keys and values are separated by a colon.
If you are trying to check the type of an object after you parse a JSON string, I suggest checking the constructor attribute: obj.constructor == Array || obj.constructor == String || obj.constructor == Object. This will be a much faster check than typeof or instanceof.
Our JSON file will look something like this: To test e.g. the color of our second item, we will to combine two mentioned approaches - referencing array item & referencing object key: With cars [1], we are selecting the second item from our array of objects, and within that object, we are using .color to select the proper value from that object.
You can decide one to use like this:
<script>window.JSON || document.write('<script src="js/json2.js"><\/script>')</script>
This checks for window.JSON
(supported by browser) first if it exists, use that else imports json2.js of Crockford.
var whichJSON = null;
if (! window.JSON) {
document.write('<script src="js/json2.js"><\/script>');
whichJSON = 'Crockford Version';
}
else {
whichJSON = 'Browser Native Version';
}
alert(whichJSON);
Before you load Crockford's script, you can check for the global JSON object exactly like he does:
<script>
var JSON,
nativeJSON = true;
if (!JSON) {
var nativeJSON = false;
document.write('<script src="js/json2.js"><\/script>');
}
if (!nativeJSON) {
// All JSON objects are using Crockford's implementation
} else {
// All JSON objects from here on out are NOT using Crockford's implementation
}
</script>
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