40

In my code, all of the info from a Postgres table row are stringified when a specific rowID 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 might be a simple way to completely exclude the columns containing null values - a function that roughly looks like this:

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

So that 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
}
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
iskandarblue
  • 7,208
  • 15
  • 60
  • 130
  • Where is the query that retrieves the resultset? – Evan Carroll Dec 13 '16 at 07:48
  • What version of javascript too, or node? – Evan Carroll Dec 13 '16 at 07:55
  • this includes node too `var url = '/api/parcels/'+layerName+'/parcel/'+parcelKey;
 makeAjaxRequest(url, params = {})
 .done(function(result) {
 var jsonRes = result.message.rows; if (features.length) { // Get coordinates from the symbol and center the map on those coordinates map.flyTo({center: e.lngLat}); console.log(e.lngLat); }` – iskandarblue Dec 13 '16 at 08:04
  • possible duplicate of http://stackoverflow.com/questions/26540706/preserving-undefined-that-json-stringify-otherwise-removes – TechnoCrat Dec 13 '16 at 13:12

4 Answers4

61

You can do something like this:

let x = {
  'x1': 0,
  'x2': null,
  'x3': "xyz", 
  'x4': null
}

console.log(JSON.stringify(x, (key, value) => {
  if (value !== null) return value
}))
Masoud
  • 1,008
  • 1
  • 9
  • 22
  • 9
    Modern syntax allows for a nice one-liner: `JSON.stringify(x, (k, v) => v ?? undefined)` – l33t Sep 18 '21 at 19:55
26

Thanks for the replies. 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>'
;
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
iskandarblue
  • 7,208
  • 15
  • 60
  • 130
  • 1
    Perfect answer. I already had a replacer to strip out circular references, so I just added the null check to that. – Wade Hatler Feb 03 '20 at 18:49
0

Try this:

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);
}
Slava Utesinov
  • 13,410
  • 2
  • 19
  • 26
0

If you want to keep your initial 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);
Weedoze
  • 13,683
  • 1
  • 33
  • 63