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?
You could use:
The
stringifymethod of theJSONobject turns an object into a string. The empty object{}will turn in"{}". If you want to add an empty string instead, use: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 ofinnerValue.An alternative method to check whether an object is empty or not: