Node js JSON cleaning

Viewed 6368

I want to remove all keys that have values of N/A, - or an empty string. If one of these values appear in the array the I want to remove that single item from the array. So for instance my example output is this:

{"name":{"first":"Daniel","middle":"N/A","last":"Smith"},"age":45}

The output I expect is this

{"name":{"first":"Daniel","last":"Smith"},"age":45}

I have tried this but it does not seem to work:

function recurse(resp) {
  let data;
  for (var x in resp) {
    data = resp[x]

    if (data === 'null' || data === null || data === '-' || typeof data === 'undefined' || (data instanceof Object && Object.keys(data).length == 0)) {
      delete resp[x];
    }

    if (data instanceof Object) {
      data = recurse(data);
    }
  }

  return resp;
}
4 Answers

I added some additional checks and seem to be working fine for more nested objects.

let obj = {"name":{"first":"Daniel","middle":"N/A","last":"Smith", "innerObject":{"prop1":"random value", "prop2":null, "prop3":"N/A", "prop4":{}}},"age":45};

function recurse(data){
for(let key in data){
        if(data[key] instanceof Object){
            if(Object.keys(data[key]).length == 0){
                delete data[key];
            } else {
                recurse(data[key]);
            }
        } else if(data[key] === 'null' || data[key] == 'N/A' || data[key] === null || data[key] === '-' || typeof data[key] === 'undefined' || (data[key] instanceof Object && Object.keys(data[key]).length == 0)){
            delete data[key];
        }
    }
return data;
}
let newobj = recurse(obj)
console.log(newobj)

Please let me know if it works for you! Thanks

It is working, just need to add the N/A validation

let obj = {"name":{"first":"Daniel","middle":"N/A","last":"Smith"},"age":45};

function recurse(resp) {
  let data;
  for (var x in resp) {
    data = resp[x]

    if (data === 'null' || data == 'N/A' || data === null || data === '-' || typeof data === 'undefined' || (data instanceof Object && Object.keys(data).length == 0)) {
      delete resp[x];
    }

    if (data instanceof Object) {
      data = recurse(data);
    }
  }

  return resp;
}

let newobj = recurse(obj)
console.log(newobj)

In order to avoid the max stack size exceeded error, the code will need to be refactored to evaluate the object iteratively. I have adjusted the code to include the suggestion that @Carlos1232 had, and also iterate over the values on the object.

function clean(resp) {
    const queue = [resp];

    while (queue.length) {
        const element = queue.pop();

        for (var x in element) {
            const data = element[x];

            if (data === 'null' || data == 'N/A' || data === null || data === '-' || typeof data === 'undefined' || (data instanceof Object && Object.keys(data).length == 0)) {
                delete element[x];
            }
            
            if (data instanceof Object) {
                queue.push(data);
            }
        }
    }

    return resp;
}

you will need to write a recursive function to walk the json tree of nested dictionaries to make it generic. I am demonstrated how to delete an element from an nested dictionary using to keys: the parent key and the child key.

 myjson={"name":{"first":"Daniel","middle":"N/A","last":"Smith"},"age":45}
 to_delete=[]
 for key,item in myjson.items():
      if isinstance(item,dict):
          for key2,element in item.items():
              print(key,key2,element)
              if element=='N/A':
                  to_delete.append({'key':key,'key2':key2})
      else:
           print(key,item)

  print(to_delete)

  for item in to_delete:
        print(item)
        del myjson[item['key']][item['key2']]


  print(myjson)
Related