Inside my loop I am creating JSON objects like so
JSONObject = {
"manufacturer": JSON.parse(object.deviceSpecificationJson[0]).manufacturer,
"model": JSON.parse(object.deviceSpecificationJson[0]).model,
"capacity": object.capacity[0],
// etc
}
But node throws an error when capcity is undefined, some of the devices in my XML which I converted to json may not contain a value in the capacity field.
How do I tell node to keep going even if the field is undefined?
4 Answers 4
In JavaScript, you can use the ternary operator on object.capacity, like so:
object.capacity ? object.capacity[0] : null. This checks whether object.capacity exists, and executes the line after ? if it does, and executes the line after : if it does not.
More generally, this is how you use the ternary operator, {condition} ? {if true} : {otherwise}
An alternative would be to define a function that checks for object.capacity and returns null or whatever default value you want the object property to have if it does not exist.
I have modified your example with the changes below:
JSONObject = {
"manufacturer": JSON.parse(object.deviceSpecificationJson[0]).manufacturer,
"model": JSON.parse(object.deviceSpecificationJson[0]).model,
"capacity": object.capacity ? object.capacity[0] : null,
// null can be replaced with the value you want it to show if the property does not exist
// like 0, or an empty string ''
}
Comments
Easiest way i can think of is to use && and ||
JSONObject = {
"manufacturer": JSON.parse(object.deviceSpecificationJson[0]).manufacturer,
"model": JSON.parse(object.deviceSpecificationJson[0]).model,
"capacity": (object && object.capacity[0]) || 'value in case object.capacity is undefined',
// etc
}
5 Comments
(object && object.capacity[0]) what is the object && part for?object && checks for the object existence. so if object is a falsy value than the object.capacity[0] will not be executedresult.capacity[0] would it then be (result && result.capacity[0])short circuiting which is same as logical and gate. where if any of the input is false output turns out to be false. irrespective of other input"storageSize": (product && product.vertical[0].storageSize[0]) || "" and got the same error Cannot read property '0' of undefined ?You'll need to put one condition to check undefined
JSONObject = {
"manufacturer": JSON.parse(object.deviceSpecificationJson[0]).manufacturer,
"model": JSON.parse(object.deviceSpecificationJson[0]).model,
"capacity": object.capacity[0]!= undefined ? object.capacity[0]: 'your static value for undefined capacity like null' ,
// etc
}
Comments
You can conditionally add capacity to your JSONObject like so:
JSONObject = {
"manufacturer": JSON.parse(object.deviceSpecificationJson[0]).manufacturer,
"model": JSON.parse(object.deviceSpecificationJson[0]).model,
// etc
}
if(object.capacity[0]) JSONObject["capacity"] = object.capacity[0]
Or, if you just want to set the capacity to be 0GB if it doesn't exist you can do the following:
JSONObject = {
"manufacturer": JSON.parse(object.deviceSpecificationJson[0]).manufacturer,
"model": JSON.parse(object.deviceSpecificationJson[0]).model,
"capacity": object.capacity[0] || '0GB',
// etc
}