2

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"}]

4
  • unjson, remove null, json again? Commented Apr 23, 2015 at 10:02
  • 1
    javascript solution to your question: stackoverflow.com/questions/281264/… Commented Apr 23, 2015 at 10:02
  • You can try this on:- jsonstring=JSON.stringify(jsonstring).replace(/null/g,' '); jsonstring.replace(/ ,/g,'') Commented Apr 23, 2015 at 10:29
  • Please give the example for my jsonstring Commented Apr 23, 2015 at 10:45

3 Answers 3

4
var arr = JSON.parse(json_string);
arr = arr.filter(function(n){ return n }); 
json_string = JSON.stringify(arr)
Sign up to request clarification or add additional context in comments.

2 Comments

Please give the example for my jsonstring
just replace json_string with your actual JSON string
2

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);

2 Comments

Copied from here? Change variable name atleast! :P
Please give the example for my jsonstring
1

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

2 Comments

Please give the example for my jsonstring
you have the input json string with you rite?

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.