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?

Jon
  • 38,814
  • 81
  • 233
  • 382
  • 2
    in fact, it's not `empty JSON array` but `empty JSON object`, difference is in brackets `[]`(array), `{}`(object) – Marek Sebera Oct 03 '11 at 12:12
  • In JSON, an array is represented by `["value0", "value1"]` and an object by `{"key0": "value0", "key1": "value1"}` http://www.json.org/ – Pascal Qyy Oct 03 '11 at 12:13

6 Answers6

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);
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Will this also work? `if (!innerValue) { innerArr.push(innerValue); }` – Ali Khalid Oct 03 '11 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. – Rob W Oct 03 '11 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? – Rob W Oct 03 '11 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);
});
scessor
  • 15,995
  • 4
  • 43
  • 54
1

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

var obj = jQuery.parseJSON('{"ST":"Security"}');
alert( obj.ST === "Security" );
gtamil
  • 532
  • 5
  • 10
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);
Community
  • 1
  • 1
vzwick
  • 11,008
  • 5
  • 43
  • 63
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.

stivlo
  • 83,644
  • 31
  • 142
  • 199
0
$.each(data, function(key, value) {
    var innerArr = [];
    $.each(value, function(innerKey, innerValue) {
        innerArr.push(innerValue);
    });
    //valueArr.push(innerArr);
    valueArr.push(innerArr.join());
});
wong2
  • 34,358
  • 48
  • 134
  • 179