2
  • How can i remove the key's with null values and form a new json.
  • How can iterate this response.... where i want to display.. this key ==> corresponding value.
John Cooper
  • 6,277
  • 26
  • 72
  • 97

5 Answers5

5

1: This will remove any falsey value i.e. Null, undefined or empty strings. You could specifically check for nulls though. Make sure you read up and understand what delete does, it gets a lot of people in trouble.

for(var key in someObject) {
    if(!someObject[key]) {
        delete someObject[key];
    }
}

2: You can iterate all properties and values of objects like so:

for(var key in someObject) {
    console.log("The value of " + key + " is " + someObject[key]);
}
Tyler
  • 2,419
  • 4
  • 20
  • 29
2

For starters fix your javascript object as what you have posted is full of errors. Once you have a valid array:

var values = [{
    'SPO2': 222.00000,
    'VitalGroupID': 1152,
    'Temperature': 36.6666666666667,
    'DateTimeTaken': '/Date(1301494335000-0400)/',
    'UserID': 1,
    'Height': 182.88,
    'UserName': null,
    'BloodPressureDiastolic': 80,
    'Weight': 100909.090909091,
    'TemperatureMethod': 'Oral',
    'Resprate': null,
    'HeartRate': 111,
    'BloodPressurePosition': 'Standing',
    'VitalSite': 'Popliteal',
    'VitalID': 1135,
    'Laterality': 'Right',
    'HeartRateRegularity': 'Regular',
    'HeadCircumference': '',
    'BloodPressureSystolic': 120,
    'CuffSize': 'XL'
}, {
    'SPO2': 100.00000,
    'VitalGroupID': 1113,
    'Temperature': 32.7777777777778,
    'DateTimeTaken': '/Date(1299856980000-0500)/',
    'UserID': 1,
    'Height': 0,
    'UserName': 'Admin',
    'BloodPressureDiastolic': 78,
    'Weight': 49895.1607,
    'TemperatureMethod': '',
    'Resprate': null,
    'HeartRate': null,
    'BloodPressurePosition': 'Sitting',
    'VitalSite': '',
    'VitalID': 1096,
    'Laterality': '',
    'HeartRateRegularity': 'Regular',
    'HeadCircumference': '',
    'BloodPressureSystolic': 120,
    'CuffSize': ''
}];

then you could loop through it:

for (var i = 0; i < values.length; i++) {
    // this will run for each element of the initial array

    for (var propertyName in values[i]) {
        // this will run for each property of the element
        var propertyValue = values[i][propertyName];

        if (propertyValue == null) {
            // if the value is null remove it
            delete values[i][propertyName];
        } else {
            console.log('name: ' + propertyName + ', value: ' + propertyValue);   
        }
    }
}

Demo.

Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
  • @John Cooper, see here: http://stackoverflow.com/questions/126100/how-to-efficiently-count-the-number-of-keys-properties-of-an-object-in-javascript – Darin Dimitrov Jul 17 '11 at 11:42
1

Deleting a property:

if (objectName.propertyName === null) {
    delete objectName.propertyName;
}

Iterating throw properties:

for (var key in objectName) {
    document.write(objectName[key]);
}
Karolis
  • 8,967
  • 26
  • 38
1
var array_of_json_hashes;

var result = [];

for(var i = 0; i < array_of_json_hashes.length; i++) {
  result[i] = {};
  var h = array_of_json_hashes[i];
  for (var key in h) {
    console.log(key);
    console.log(h[key]);
    if (h.hasOwnProperty(key)) {
      if(h[key]) {
        result[i][key] = h[key];
      }
    }
  }
}

console.log(result);
stef
  • 13,499
  • 2
  • 42
  • 66
0

This may be helpful for you:

1) test = ["1","2","3","4",""," "];
2) var delete = JSON.stringify(test);

case1) delete = delete.replace(/\,""/g,'');
or
case2) delete = delete.replace(/\," "/g,'');
or
case3) delete = delete.replace(/\,null/g,'');

3) var result = JSON.parse(delete);

sorak
  • 2,587
  • 2
  • 14
  • 24
Vishal Kumar
  • 792
  • 8
  • 10