Hide null values ​​in JSON.stringify () output

In my code, all information from a row in a Postgres table is gated when a particular row id is selected.

var jsonRes = result.message.rows; document.getElementById('panel').innerHTML = '<pre>' + JSON.stringify(jsonRes[0], null, "\t") + '</pre>' 

The result looks something like this:

 { "ogc_fid": 143667, "relkey": 288007, "acct": "000487000A0010000", "recacs": "12.5495 AC", "shape_star": 547131.567383, "shape_stle": 3518.469618, "objectid": 307755, "zone_dist": "MU-3", "pd_num": null, "council_da": null, "long_zone_": "MU-3", "globalid": "{D5B006E8-716A-421F-A78A-2D71ED1DC118}", "ord_num": null, "notes": null, "res_num": null, "effectived": 1345766400000, "shape.star": 629707.919922, "shape.stle": 3917.657332, "case_numbe": null, "common_nam": null, "districtus": null } 

I am new to JS and would like to know if there could be an easy way to completely exclude columns with null values ​​- a function that looks something like this:

 function hide(jsonObject) { if (property === null) { hide property } else { return str } } 

So, in the end, the object in the panel looks like this:

 { "ogc_fid": 143667, "relkey": 288007, "acct": "000487000A0010000", "recacs": "12.5495 AC", "shape_star": 547131.567383, "shape_stle": 3518.469618, "objectid": 307755, "zone_dist": "MU-3", "long_zone_": "MU-3", "globalid": "{D5B006E8-716A-421F-A78A-2D71ED1DC118}", "effectived": 1345766400000, "shape.star": 629707.919922, "shape.stle": 3917.657332 } 
+6
source share
4 answers

You can do something like this:

 function replacer(key,value) { if (value === null) return undefined return value } var x = { 'x1':0, 'x2':null, 'x3':"xyz", 'x4': null, 'x5': null }; console.log(x) console.log(JSON.stringify(x, replacer)); 
+8
source

Thanks for answers. I just realized that JSON.stringify () has a REPLACER parameter ( info here )

So, I just added:

 function replacer(key, value) { // Filtering out properties if (value === null) { return undefined; } return value; } document.getElementById('panel').innerHTML = '<pre>' + JSON.stringify(jsonRes[0], replacer, "\t") + '</pre>' ; 
+1
source

Try the following:

 function getCleanObject(jsonObject) { var clone = JSON.parse(JSON.stringify(jsonObject)) for(var prop in clone) if(clone[prop] == null) delete clone[prop]; return JSON.stringify(clone); } 
0
source

If you want to keep your original object, you can create a new one like this

  var object = { "ogc_fid": 143667, "relkey": 288007, "acct": "000487000A0010000", "recacs": "12.5495 AC", "shape_star": 547131.567383, "shape_stle": 3518.469618, "objectid": 307755, "zone_dist": "MU-3", "pd_num": null, "council_da": null, "long_zone_": "MU-3", "globalid": "{D5B006E8-716A-421F-A78A-2D71ED1DC118}", "ord_num": null, "notes": null, "res_num": null, "effectived": 1345766400000, "shape.star": 629707.919922, "shape.stle": 3917.657332, "case_numbe": null, "common_nam": null, "districtus": null }; var newObj = {}; Object.keys(object).forEach(function(key) { if (object[key] !== null) newObj[key] = object[key]; }); console.log(newObj); 
-one
source

Source: https://habr.com/ru/post/1013119/


All Articles