I have a JSON object in Javascript like given below:
var myjson={"name":"myname","address":"Myaddress"}
// if my json is not empty:
//do something
I want to find if the object is empty, that is if there are any keys present. How can I do that in Javascript?
Searched in vain through Google Search, but it is tough to say what to search for with so much junk (irrelevant) information coming up.
Is there any official reference for how this works (including delete a key) ? Or am I supposed to fight with Google Search (no bling for me) and hope for some relevant information?
If you have ECMAScript 5:
Object.keys(myjson).length
will tell you how many (enumerable) properties exist in the object.
See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys, where there's also a version you can put in your own code if your browser doesn't support it natively.
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