2

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"
    }
  ]
}
pete
  • 355
  • 3
  • 14
  • 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 Dec 02 '17 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 Dec 02 '17 at 22:30
  • @MatiasCicero Relax, it's from a JSON file, so once it is parsed, it's already a copy. – blex Dec 02 '17 at 22:33
  • do you get the object from JSON.parse ? – Slai Dec 02 '17 at 22:34
  • I have already put the json file that I would wait after removing the nulls – pete Dec 02 '17 at 22:34
  • @Slai I get the json file from JSON.stringify – pete Dec 02 '17 at 22:36
  • 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 Dec 02 '17 at 22:36
  • Possible duplicate of [Recursively remove null values from JavaScript object](https://stackoverflow.com/questions/18515254/recursively-remove-null-values-from-javascript-object) – Hjulle Dec 02 '17 at 22:55
  • That's not an [mcve] as it doesn't have the definition of `create` and `aux` and has extra get and set methods that don't seem necessary. Code that results in the JSON sample is needed for us to reproduce the issue (you can use the `<>` button to create it). I am not sure why you are setting then to `null` in the first place `new Array(null,null,null)` ? – Slai Dec 03 '17 at 00:35
  • @Slai I did not want to overload code sorry, the create method is somewhat extensive.But in practice I simply simulate a ternary tree, the variable aux corresponds to the name of the node. I initialize it in null since I can not assign a value of 3 to the array. – pete Dec 03 '17 at 00:39
  • never mind .. you are stringifying a string :] .. you can try `var o = parent;` – Slai Dec 03 '17 at 00:47
  • :o My god has finally worked. Thank you for your patience and excuse the silly and novice questions. – pete Dec 03 '17 at 00:52

1 Answers1

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 )
Slai
  • 22,144
  • 5
  • 45
  • 53
  • Your solution is fantastic 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 the empty array too: I get this "{\" name \ ": \" Ann \ " , \ "children \": [null, {\ "name \": \ "Beta \", \ "children \": [null, null, null]}, null]} ". What am I doing wrong? – pete Dec 02 '17 at 23:10
  • @pete maybe the values are not `null`, but `undefined` or missing. Try the update. – Slai Dec 02 '17 at 23:14
  • Keep going the same thing is very strange. With the json burned it works. But getting it with JSON.stringify does not work. JavaScript only seeks to annoy me. – pete Dec 02 '17 at 23:23
  • @pete if the updated version doesn't work either, can you update your question with the code that reproduces the issue, or make a separate question with a [MVCE](https://stackoverflow.com/help/mcve) – Slai Dec 02 '17 at 23:32