Remove null values from json file javascript - Stack Overflow

I am new to javascript and I have encountered a problem I need to remove all null values from a json fi

I am new to javascript and I have encountered a problem I need to remove all null values from a json file. But I have not been able to get it I have tried different methods that I found on the site but they do not work for me. One of the ways I found below. I just have a problem as I said before the json file I get it with JSON.stringify and by using the code that removes null I get this "{\" name \ ": \" Ann \ " , \ "children \": [null, {\ "name \": \ "Beta \", \ "children \": [null, null, null]}, null]} ".

function Parent(name){
    this.name = name;
    this.children=new Array(null,null,null);
}

Parent.prototype.getName = function(){
return this.name;
};

Parent.prototype.setName = function(name) { 
 this.name=name; 
};

Parent.prototype.getChildren = function(){
 return this.children;
};

Parent.prototype.setChildren = function(parent) { 
 this.children=parent; 
};

var parent = create(aux,new Parent(""));// This method create tree parent
var o = parent;
j = JSON.stringify(o, (k, v) => Array.isArray(v) 
       && !(v = v.filter(e => e !== null && e !== void 0)).length ? void 0 : v, 2 )
     alert (j);

Json file:

{
  "name": "Ann",
  "children":
  [
    null,
    {
      "name": "Beta",
      "children":
      [
        null,
        null,
        null
      ]
    },
    null
  ]
}

What I expect:

{
  "name": "Ann",
  "children":
  [
    {
      "name": "Beta"
    }
  ]
}

I am new to javascript and I have encountered a problem I need to remove all null values from a json file. But I have not been able to get it I have tried different methods that I found on the site but they do not work for me. One of the ways I found below. I just have a problem as I said before the json file I get it with JSON.stringify and by using the code that removes null I get this "{\" name \ ": \" Ann \ " , \ "children \": [null, {\ "name \": \ "Beta \", \ "children \": [null, null, null]}, null]} ".

function Parent(name){
    this.name = name;
    this.children=new Array(null,null,null);
}

Parent.prototype.getName = function(){
return this.name;
};

Parent.prototype.setName = function(name) { 
 this.name=name; 
};

Parent.prototype.getChildren = function(){
 return this.children;
};

Parent.prototype.setChildren = function(parent) { 
 this.children=parent; 
};

var parent = create(aux,new Parent(""));// This method create tree parent
var o = parent;
j = JSON.stringify(o, (k, v) => Array.isArray(v) 
       && !(v = v.filter(e => e !== null && e !== void 0)).length ? void 0 : v, 2 )
     alert (j);

Json file:

{
  "name": "Ann",
  "children":
  [
    null,
    {
      "name": "Beta",
      "children":
      [
        null,
        null,
        null
      ]
    },
    null
  ]
}

What I expect:

{
  "name": "Ann",
  "children":
  [
    {
      "name": "Beta"
    }
  ]
}
Share Improve this question edited Dec 3, 2017 at 0:53 pete asked Dec 2, 2017 at 22:15 petepete 3653 silver badges14 bronze badges 12
  • Because I don't think you want to delete object properties. It looks like you need to splice out null elements of arrays instead. – Andy Commented Dec 2, 2017 at 22:25
  • Are you sure you want to modify the original object? I'd consider creating a new filtered object instead of updating and deleting properties from the original reference – Matias Cicero Commented Dec 2, 2017 at 22:30
  • @MatiasCicero Relax, it's from a JSON file, so once it is parsed, it's already a copy. – blex Commented Dec 2, 2017 at 22:33
  • do you get the object from JSON.parse ? – Slai Commented Dec 2, 2017 at 22:34
  • 1 @blex From the point of view of the method, this can be any object. I'm just saying so the caller of the method does not get any unexpected behavior. Immutability over mutability, if possible. – Matias Cicero Commented Dec 2, 2017 at 22:36
 |  Show 7 more ments

1 Answer 1

Reset to default 7

JSON.parse and JSON.stringify accept replacer function to modify the values:

j = '{ "name": "Ann", "children": [ null, { "name": "Beta", "children": [ null, null, null ] }, null ] }'

o = JSON.parse(j, (k, v) => Array.isArray(v) ? v.filter(e => e !== null) : v )

console.log( o )

o = { "name": "Ann", "children": [ null, { "name": "Beta", "children": [ null, null, null ] }, null ] }

j = JSON.stringify(o, (k, v) => Array.isArray(v) ? v.filter(e => e !== null) : v, 2 )

console.log( j )

To remove the empty array too:

o = { "name": "Ann", "children": [ null, { "name": "Beta", "children": [ null, null, null ] }, null ] }

j = JSON.stringify(o, (k, v) => Array.isArray(v) 
                                && !(v = v.filter(e => e)).length ? void 0 : v, 2 )

console.log( j )

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744048886a4549700.html

相关推荐

  • Remove null values from json file javascript - Stack Overflow

    I am new to javascript and I have encountered a problem I need to remove all null values from a json fi

    9天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信