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

**URL:** <https://community.postman.com/t/clear-delete-hide-null-values-from-json-response-body-for-better-readability/17069>\
**Category:** Help Hub\
**Tags:** scripts, json, test\
**Created:** [October 24, 2020, 9:57pm UTC](https://community.postman.com/t/clear-delete-hide-null-values-from-json-response-body-for-better-readability/17069 "2020-10-24T21:57:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastian](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@Sebastian](https://community.postman.com/u/Sebastian)\
**Post date:** [October 24, 2020, 9:57pm UTC](https://community.postman.com/t/clear-delete-hide-null-values-from-json-response-body-for-better-readability/17069/1 "2020-10-24T21:57:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [October 26, 2020, 11:48am UTC](https://community.postman.com/t/clear-delete-hide-null-values-from-json-response-body-for-better-readability/17069/2 "2020-10-26T11:48:56Z")

</div>

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

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![Sebastian](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@Sebastian](https://community.postman.com/u/Sebastian)\
**Post date:** [October 26, 2020, 4:00pm UTC](https://community.postman.com/t/clear-delete-hide-null-values-from-json-response-body-for-better-readability/17069/3 "2020-10-26T16:00:24Z")

</div>

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?

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/7/d/7dca8070a0222f56af349747aff6151c2c5bc172.png)

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [October 26, 2020, 5:08pm UTC](https://community.postman.com/t/clear-delete-hide-null-values-from-json-response-body-for-better-readability/17069/4 "2020-10-26T17:08:21Z")

</div>

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