Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone, good morning.

First of all, i am a begginer to this subjects and English is not my native language.

Thx for the help.

I am developing a test script in javascript which gets values from a JSON response.

See the example below:

HTTP/1.1 200 OK
Content-Type: application/vnd.api+json

{
    "data": [{
        "type": "articles",
        "id": "1",
        "attributes": {
            "title": "JSON API paints my bikeshed!",
            "body": "The shortest article. Ever.",
            "created": "2015-05-22T14:56:29.000Z",
            "updated": "2015-05-22T14:56:28.000Z"
        },
        "relationships": {
            "author": {
                "data": {"id": "42", "type": "people"}
            }
        }
    }],
    "included": [
    {
        "type": "people",
        "id": "42",
        "attributes": {
            "name": "John",
            "age": 80,
            "gender": "male"
        }
    }
    ]
}


However, for some reason, some variables that should come in my JSON script are missing and for that reason my assertions are failing. (imagine for example that i will assert the gender from the response with the expected gender, but the response gender is missing as if the code didn`t even had gender from the start).

I believe it happens because the gender camp is optional.

In JAVA/groovy i know that i could use ? to ignore the missing variables... How can i do the same in javascript.

What I have tried:

i am trying with If/Else, but i`ve failed. Anyway i want to use a shorter solution and more optimized to solve that problem. The closer i get from ? the better,
Posted
Updated 2-Mar-18 4:05am
v2
Comments
F-ES Sitecore 2-Mar-18 9:31am    
You can test the "gender" field to see if it exists like

if (yourObj.gender) {
// validate the gender field
}
else {
// no gender field existed
}
Member 13705226 2-Mar-18 10:41am    
so, for each assertion i make i need to validate it`s existance?
F-ES Sitecore 5-Mar-18 3:59am    
If they are optional, then yes.
Member 13705226 6-Mar-18 6:33am    
IN groovy i saw the use of the "?" in the code to evaluate those cases removing the necessity of creating IF ELSES. Some libary from JS has this function?
F-ES Sitecore 6-Mar-18 6:49am    
If you have to do this a lot I'm sure you could write your own function. js lets you access properties programatically

var x = { "a": 1, "b": 2 };
alert(getOrDefault(x, "a", 0)); // 1
alert(getOrDefault(x, "b", 0)); // 2
alert(getOrDefault(x, "c", 0)); // 0

function getOrDefault(obj, propName, defaultyValue)
{
if (obj[propName] === undefined) {
return defaultyValue;
}
else {
return obj[propName];
}
}

https://ponyfoo.com/articles/null-propagation-operator

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900