View Full Version : Resolved 3 JSON basic questions

10-17-2008, 03:45 PM
It might look like elementary JSON issues but: does anyone know how to:

1. Detect if a JSON object is empty
2. Remove a property (along with its value) from a JSON object
- and related with the second question:
3. Clear the JSON object from all his properties (clear the object)


10-17-2008, 08:01 PM
1) I would try something like:

Object.prototype.isEmpty = function() {
for (var prop in this) {
if (this.hasOwnProperty(prop)) return false;
return true;

2) There is a delete keyword:

var obj = { a: 1, b: 2 };
delete obj.a;
alert("a" in obj); // false

3) I would a combination of (1) and (2)

Object.prototype.clearProperties = function() {
for (var prop in this) {
if (this.hasOwnProperty(prop))
delete this[prop];

(Of course, why not simply discard the populated object and initialize a new one?)

10-17-2008, 08:24 PM
Thanks. It should have been obvious for me from the beginning but, for a reason or another, I was initially handled the prototype in a wrong manner without any result.