0

I'm trying to create a function that clears up a JSON object by removing jQuery objects and null objects, strings.

CODE:

JSON.clean = function (object) {
    /// <summary>Removes jQuery and null values from json object.</summary>
    var filter = function (obj, doArrays) {
        $.each(obj, function (key, value) {                
            if (value === "" || value === null) {
                delete obj[key];                
            } else if (Object.prototype.toString.call(value) === '[object Object]') {
                filter(value);
            } else if (doArrays || Array.isArray(value)) {
                obj[key] = filterArray(value);
            }
        });
        return obj;
    };

    var filterArray = function (obj) {
        var result = [];
        for (var i = 0; i < obj.length; i++) {
            result.push(filter(obj[i], false));
        }
        return result;
    };

    var result;
    if ($.isArray(object)) {
        result = [];
        for (var i = 0; i < object.length; i++) {
            result.push(filter(object[i]));
        }            
    } else {
        result = filter(object);
    }
    return result;
};


PROBLEM:
The code crashes when trying to clean up arrays within the JSON object.
I know that it's wrong to alter a Array like this but this is purely meant for JSON objects with in an array.
My suggestion to this problem would be to wait untill the cleaning of an Array is completed.
I don't know how to achieve this in Javascript so i'm hoping anyone of you can help!

I've used this source for the main idea:
How do I remove all null and empty string values from a json object?

Community
  • 1
  • 1
Jeroen Vorsselman
  • 803
  • 1
  • 9
  • 19
  • What does *"the code crashes"* mean? Does the browser crash? Does the code abruptly end with an error? –  Sep 29 '14 at 17:56
  • ...and it seems like you're dealing with JavaScript objects, not JSON since you can't represent specific object types in JSON other than Array objects. –  Sep 29 '14 at 17:57
  • When i run this code using jQuery 2.1.0 i get the error a is undefined in the javascript library, this occurs when trying to call the recursive function for cleaning up the Array. – Jeroen Vorsselman Sep 29 '14 at 21:14

2 Answers2

0

You can use JSON.stringify , replacer method to convert to string and remove null, then convert back to JSON from string.

  1. convert object(having null value ) to string = JSON.stringify(obj,param...);
  2. back to object = JSON.parse("string value");

Some helpful link

  1. Recursively remove null values from JavaScript object
  2. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
Community
  • 1
  • 1
Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
  • That will do more than just remove `null` and jQuery objects. And doesn't a full serialization followed by parsing the result seem like overkill? –  Sep 29 '14 at 17:55
  • @squint, it depends on object size. I was just proposing a way to achieve. If object size is less ,and has more number of null values, then i think , this good way to go, what say? – Arindam Nayak Sep 29 '14 at 17:57
  • Yes, for small objects the difference wouldn't be noticed. –  Sep 29 '14 at 18:00
-1
var reqArr = {
    "a": {
    "id": "1",
    "name": '',
    "first_name": "Jason",
    "last_name": "Davis",
    "is_admin": "1",
    "gravatar": "31b64e4876d603ce78e04102c67d6144"
},
"b": '',
"c": {
    "id": "1702c3d0-df12-2d1b-d964-521becb5e3ad",
    "name": "Jeff",
    "first_name": "",
    "last_name": "",
    "is_admin": "1",
    "gravatar": "5359bf585d11c5c35602f9bf5e66fa5e"
    }
};

for(var key in reqArr) {
    if (reqArr[key] == undefined || reqArr[key] == '') {
        delete reqArr[key];
    }
    for(var inKey in reqArr[key]) {
        if (reqArr[key][inKey] == undefined || reqArr[key][inKey] == '') {
            delete reqArr[key][inKey];
        }
    }
}
Karl Gjertsen
  • 4,690
  • 8
  • 41
  • 64
Vasanth
  • 212
  • 1
  • 8