Clear/delete/hide null values from JSON response body for better readability

I am looking for an easy solution (preferably with test-script) to clear/delete/hide null values from JSON response. Objective is solely on improved readability of my POST responses, as I am dealing with +1500 lines of which 30% are empty fields.

Response will not be further used in in subsequent requests, hence no attention to syntax required.

Thank you so much - I have been browsing the google for many hours and have not found any help.

I haven’t tried this code, but you could try something like this:

let jsonData = pm.response.json();

jsonData = clearBlankValues(jsonData);
console.log(jsonData);

function clearBlankValues(obj) {
  const keys = Object.keys(obj);  
  for(let i = 0; i < keys.length; i++) {
    const key = keys[i];
    const param = obj[key]
    if(!param) {
      delete obj[key];
    }
    else if (typeof param === 'object') {
      if(Object.keys(param).length) {
        clearBlankValues(param);
      }
      else {
        delete obj[key];
      }
    }
    else if (typeof param === 'array') {
      if(!param.length){
        delete obj[key]
      }
      else {
        param.forEach(value, index => {
          if(typeof value === 'object'){
            if(Object.keys(value).length){
              value = clearBlankValues(value);
            }
          }
        })
        for(let index = 0; index < param.length; index++) {
          if(typeof param[index] === 'object'){
            clearBlankValues(param[index]);
          }
        }
      }
    }
  }

  return obj;
}

Hi @allenheltondev thank you for your reply and help!!

The script works and I can see the response without null values in the console log. Unfortunately the response is displayed in a fully collapsed object structure. It is very inconvenient to analyze the data by expanding each object individually by mouse click.

Is it possible to parse the script (data without null values) in plain JSON code?

You could set it to an environment variable, then copy and paste the value into a json formatter