1

I have a JSON object, which includes other objects and list of objects. Have to write a function, which iterates through all properties of objects as well as object within object and list of objects and replace null with an empty string.

As it is loop inside loop, I need to implement deferred so sequential processing. I tried many ways, but failed. Anyone please help.

function ValidateObject(result) {
  var aObj = result.A;
  aObj = VerifyForNull(aoBJ);
  var bObj = result.B;
  bObj = VerifyForNull(bObJ);
  for (var i = 0; i < result.C.length; i++) {
    var cObj = result.C[i];
    cObj = VerifyForNull(cObJ);
    for (var j = 0; j < cObj.D.length; j++) {
      var dObj = cObj.D[i];
      dObj = VerifyForNull(dObj);
    }
  }
}

function VerifyForNull(obj) {
  Object.keys(obj).forEach(function(key) {
    var val = obj[key];
    if (val == null || value === undefined) {
      obj[key] = "";
    }
  });
}
Rick
  • 4,030
  • 9
  • 24
  • 35
ashwinrajagopal
  • 27
  • 1
  • 1
  • 5
  • 1
    Could you please give a sample of the input object and the output you're trying to create. It looks like you could quite easily simplify this by using `||` as a null coalescing operator. Also, recursion would help a lot here, and make the logic far more extensible. – Rory McCrossan Jul 25 '18 at 07:31
  • https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json – Teemu Jul 25 '18 at 07:39
  • { "person": { "id": 12345, "name": "John Doe", "phones": { "home": "800-123-4567", "mobile": "null" }, "email": [ "jd@example.com", "jd@example.org" ], "dateOfBirth": "null", "registered": true, "emergencyContacts": [ { "name": "Jane Doe", "phone": "null", "relationship": "spouse" }, { "name": "Justin Doe", "phone": "877-123-1212", "relationship": "null" } ] } } – ashwinrajagopal Jul 25 '18 at 07:47
  • { "person": { "id": 12345, "name": "John Doe", "phones": { "home": "800-123-4567", "mobile": "" }, "email": [ "jd@example.com", "jd@example.org" ], "dateOfBirth": "", "registered": true, "emergencyContacts": [ { "name": "Jane Doe", "phone": "", "relationship": "spouse" }, { "name": "Justin Doe", "phone": "877-123-1212", "relationship": "" } ] } } – ashwinrajagopal Jul 25 '18 at 07:48
  • null should be replaced by empty string . when I am trying to iterate thru each object within root object and verify using the method VerifyForNull I am getting error as javascript doesn't wait . so wanted to use deferred but I am not much aware of deferred. – ashwinrajagopal Jul 25 '18 at 07:50

2 Answers2

7

You can use JSON.Stringify (see MDN) with a replacer method to replace all nulls in an Object:

console.log(replaceNull({
    x: {},
    y: null,
    z: [1,null,3,4],
    foo: "foo",
    bar: {foobar: null}
  }));

const yourObj = { "person": { "id": 12345, "name": "John Doe", "phones": { "home": "800-123-4567", "mobile": null }, "email": [ "jd@example.com", "jd@example.org" ], "dateOfBirth": null, "registered": true, "emergencyContacts": [ { "name": "Jane Doe", "phone": null, "relationship": "spouse" }, { "name": "Justin Doe", "phone": "877-123-1212", "relationship": undefined } ] } };

console.log(replaceNull(yourObj, ""));

function replaceNull(someObj, replaceValue = "***") {
  const replacer = (key, value) => 
    String(value) === "null" || String(value) === "undefined" ? replaceValue : value;
  //^ because you seem to want to replace (strings) "null" or "undefined" too
  
  return JSON.parse( JSON.stringify(someObj, replacer));
}
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • This is a good approach and eliminates the recursive aspect, but the downside is that `JSON.stringify` removes keys that have a value of `undefined`, which Asker wants to replace, too, judging by his code. – FatalMerlin Jul 25 '18 at 07:57
  • 1
    @FatalMerlin edited: now it replaces `undefined` too – KooiInc Jul 25 '18 at 08:49
  • Thank you very much . This resolved my issue . Like if I am trying to iterate thru Object using Object.keys(obj).forEach(function (key) then deferred need to be used which is very complex as there and objects and list of objects inside a Object . So replacing with your solution solved it . – ashwinrajagopal Jul 25 '18 at 09:13
1

UPDATE: Since your example objects have keys with value "null" which are Strings and not Objects with value null i updated my answer to correspond your needs.

Try to solve the problem recursive. Everytime the algorithm finds an object in the object the validation routine is called upon this 'new' object.

Here is my fiddle which illustrates a solution: https://jsfiddle.net/vupry9qh/13/

function isNull(obj, key) {
    return (obj[key] == null || obj[key] === undefined || obj[key] === "null");
}

function validate(obj) {
    var objKeys = Object.keys(obj);
  objKeys.forEach((key) => {
    if(isNull(obj, key)) {
        obj[key] = "";
    }
    if(typeof(obj[key]) == "object") {
        validate(obj[key]);
    }
  });
}
Thieri
  • 203
  • 1
  • 10