03-14-2021 23:15 - edited 03-14-2021 23:16
03-14-2021 23:15 - edited 03-14-2021 23:16
What is the best [foolproof] technique for handling obtaining values from a JSON object that haven't been added yet without getting undefined or an error?
EG.
let myName = "Dummy"; // initialized value
myName = obj.myName; // this value does not exist yet
Required result
myName = "Dummy"
Author | ch, passion for improvement.
03-15-2021 00:42
03-15-2021 00:42
You could try messing around with this. If you really want foolproof, you'll have to handle the case where obj itself hasn't been defined.
03-15-2021 15:26 - edited 03-15-2021 15:32
03-15-2021 15:26 - edited 03-15-2021 15:32
If an object or key could come back as undefined and that will break your code, use an if statement to explicitly check if that specific thing === undefined, and handle it as normal when it isn't undefined, and come up with whatever solution you need for when it is undefined.
For your example:
if (obj.myName === undefined) {
myName = "Dummy";
} else {
myName = obj.myName;
}
Shorthand using the ternary conditional:
myName = (obj.myName === undefined) ? "Dummy" : obj.myName;
03-17-2021 12:39
03-17-2021 12:39
Thanks to both of you.
Can you use?
if (obj.hasOwnProperty("myName") ) myName = obj.myName;
That way myName won't ever get changed to undefined if its not present in the object.
Next question, whats the best way to avoid a subsequent JSON parse error?
Eg if myName = NaN or myName = undefined
obj.myName = myName;
Will create an invalid JSON object, that can't then be parsed.
Author | ch, passion for improvement.
03-17-2021 14:24
03-17-2021 14:24
=== is an equality operator, not an assignment operator, so nothing should be "changed to undefined" - if obj.myName does not exist then (obj.myName === undefined) should return true and you will not run into the else condition and so the local myName will only be assigned the "Dummy" string. This will prevent the parse error from trying to assign an undefined value, as you will not run that code in those conditions.
obj.hasOwnPropertyName() is another way to confirm if the property is present or not. Just as long as you only run myName = obj.myName explicitly when obj.myName is known to have a value, you will avoid the JSON parse error as you won't be attempting to parse an undefined property. https://dmitripavlutin.com/check-if-object-has-property-javascript/
Finally, you can use try/catch to have your code officially handle the potential error that may occur in a specific way, such as assigning your chosen default value and sending an error or warning message of your own. https://www.w3schools.com/js/js_errors.asp
03-21-2021 12:22 - edited 03-22-2021 18:54
03-21-2021 12:22 - edited 03-22-2021 18:54
@kozm0naut Thanks, Do you think this would be a reasonable solution for retrieving variables that may or may not be there?
let myName = "Dummy"; // initialized value
getJson({myName}); // update this variable if it exists in the object
function getJson(field) {
var name = Object.keys(field)[0];
if (myObj[name.toString()] !== undefined ) eval(name + "= myObj[name.toString()]");
}
Required result
myName = "Dummy" or updated value
Author | ch, passion for improvement.