2

I have some JSON that looks like this:

{
                "ST": "Security",
                "C1": "Login failures",
                "C2": "1",
                "C3": {},
                "P1": "2",
                "P2": "administrator",
                "P3": {},
                "P4": {},
                "DESCR": "failed login attempts",
                "SID": "88",
                "AV": "NO",
                "SC": "0",
                "CN": {}
            }

I also have this jQuery loop to filter out values:

$.each(data, function(key, value) {
    var innerArr = [];
    $.each(value, function(innerKey, innerValue) {
        innerArr.push(innerValue);
    });
    valueArr.push(innerArr);
});

The problem is that on items C3, P3, P4 & CN in my example, the each loop is pushing the value [object Object] into my value collection.

Is there a way to make these items empty strings rather than objects?

2
  • 2
    in fact, it's not empty JSON array but empty JSON object, difference is in brackets [](array), {}(object) Commented Oct 3, 2011 at 12:12
  • In JSON, an array is represented by ["value0", "value1"] and an object by {"key0": "value0", "key1": "value1"} json.org Commented Oct 3, 2011 at 12:13

6 Answers 6

1

You could use:

...
if(typeof innerValue == "object") innerValue = JSON.stringify(innerValue);
valueArr.push(innerValue);
....

The stringify method of the JSON object turns an object into a string. The empty object {} will turn in "{}". If you want to add an empty string instead, use:

if(typeof innerValue == "object"){
    innerValue = JSON.stringify(innerValue);
    if(innerValue == "{}") innerValue = "";
}
valueArr.push(innerValue);

If you're 100% sure that your object is empty, you don't have to use JSON.stringify. typeof innerValue == "onject" would then be sufficient, to check whether you have to add "" instead of innerValue.

An alternative method to check whether an object is empty or not:

if(typeof innerValue == "object"){
    var isEmpty = true;
    for(var i in innerValue){
        isEmpty = false;
        break;
    }
    if(isEmpty) innerValue = "";
    else {
        //Object not empty, consider JSON.stringify
    }
}
valueArr.push(innerValue);
3
  • Will this also work? if (!innerValue) { innerArr.push(innerValue); } Commented Oct 3, 2011 at 12:16
  • An object will always evaluate to true. Non-empty strings, and integers != 0 will also evaluate to true. In short, your proposal won't work. Commented Oct 3, 2011 at 12:18
  • Using JSON.stringfy, you will only see [object Object] if a string has this content. Can you show an example where [object Object] shows up? Commented Oct 3, 2011 at 13:55
1
$.each(data, function(key, value) {
    var innerArr = [];
    $.each(value, function(innerKey, innerValue) {
        if (typeof innerValue == 'object') {
            innerValue = '';
        }
        innerArr.push(innerValue);
    });
    valueArr.push(innerArr);
});
1

FYI, you can use .parseJSON function and get results easily

var obj = jQuery.parseJSON('{"ST":"Security"}');
alert( obj.ST === "Security" );
1
$.each(data, function(key, value) {
    var innerArr = [];
    $.each(value, function(innerKey, innerValue) {
        innerValue = ($.isEmptyObject(innerValue)) ? '' : innerValue;
        innerArr.push(innerValue);
    });
    valueArr.push(innerArr);
});

Edit:

If you didn't want to rely on jQuery's isEmptyObject() function, you could implement one yourself:

Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};

// Get the size of an object
var size = Object.size(myArray);
0
$.each(data, function(key, value) {
    var innerArr = [];
    $.each(value, function(innerKey, innerValue) {
        innerArr.push(innerValue);
    });
    //valueArr.push(innerArr);
    valueArr.push(innerArr.join());
});
0

What is the inside loop for? It loops on each single letter of the String values.

$.each(data, function(key, value) {
    var innerArr = [];
    if (jQuery.isEmptyObject(value)) {
        value = '';
    }
    ...
});

Anyway you can use jQuery.isEmptyObject() to test easily if value is an empty object and modify it to an empty string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.