How to remove the null value in json string using jquery
var jsonstring=[null,null,{"rank":"23","credit":"10"},null,null,{"rank":"26","credit":"10"},null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,{"rank":"31","credit":"05"}]
How to remove the null value in json string using jquery
var jsonstring=[null,null,{"rank":"23","credit":"10"},null,null,{"rank":"26","credit":"10"},null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,{"rank":"31","credit":"05"}]
- unjson, remove null, json again? – sectus Commented Apr 23, 2015 at 10:02
- 1 javascript solution to your question: stackoverflow./questions/281264/… – Pawan Commented Apr 23, 2015 at 10:02
- You can try this on:- jsonstring=JSON.stringify(jsonstring).replace(/null/g,' '); jsonstring.replace(/ ,/g,'') – Tanul Commented Apr 23, 2015 at 10:29
- Please give the example for my jsonstring – vengatesh rkv Commented Apr 23, 2015 at 10:45
3 Answers
Reset to default 4var arr = JSON.parse(json_string);
arr = arr.filter(function(n){ return n });
json_string = JSON.stringify(arr)
Use like this
(function filter(obj) {
$.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 ($.isArray(value)) {
$.each(value, function (k,v) { filter(v); });
}
});
})(sjonObj);
You should use array access notation please see the below code
delete sjonObj[key];
$.each(sjonObj, function(key, value){
if (value === "" || value === null){
delete sjonObj[key];
}
});
Thanks