Skip to main content

hasKey

Tests if the input object contains the given key name. Returns a Boolean true or false.

The input can be a JSON object or an ATL object.

This function is typically used in conditional statements — see here for an example.

Parameters

  • OBJECT (object)

    A JSON or ATL object.

  • KEY (string)

    The key to check for.

Examples

Assume a "Describe a JSON Object" project with this data:

{
    "finances": {
        "Q1": [
            {"branch": "Austin", "sales": 2454994.45, "target": 2000000},
            {"branch": "Boston", "sales": 3485915.31, "target": 3500000},
            {"branch": "Dallas", "value": 2657920.15, "target": 2750000},
            {"branch": "Denver", "sales": 2148275.64, "target": 2000000}
        ]
    }
}

The "Q1" array contains four JSON objects. Zero-based indexing applies, so the objects are indexed 0-3.

ATL in Script

Result

[[hasKey(WholeJSON.finances.Q1[0], 'sales')]]

true

[[hasKey(WholeJSON.finances.Q1[2], 'sales')]]

false

[[hasKey(WholeJSON.finances.Q1[0], 'value')]]

false

[[hasKey(WholeJSON.finances.Q1[2], 'value')]]

true

[[hasKey(WholeJSON.finances.Q1[0], 'profit')]]

false

[[hasKey(WholeJSON.finances.Q1[2], 'profit')]]

false

The function also works on ATL objects.

ATL in Script

Result

[[

myObject = (product = 'Ointmo', Q1sales = 21624, Q2sales = 3071)

hasKey(myObject, 'Q2sales')

]]

true

[[

myObject = (product = 'Ointmo', Q1sales = 21624, Q2sales = 3071)

hasKey(myObject, 'Q3sales')

]]

false

Using hasKey in a conditional statement

Assume a "Describe a JSON Object" project with this data:

{
    "finances": {
        "Q1": [
            {"branch": "Austin", "sales": 2454994.45, "target": 2000000},
            {"branch": "Boston", "sales": 3485915.31, "target": 3500000},
            {"branch": "Dallas", "value": 2657920.15, "target": 2750000},
            {"branch": "Denver", "sales": 2148275.64, "target": 2000000}
        ]
    }
}

The "Q1" array contains four JSON objects, Note that the third object lacks a sales key.

You might write this user-defined function (UDF) script to generate a summary for each object:

#define getSummary(object)
	
[[object.branch]] achieved sales of [[abbreviateNumber(currencyFormat(object.sales))]].

This is a UDF script, so you need to call the function in another script. For example:

[[forAll(WholeJSON.finances.Q1, object -> getSummary(object))]]

This tells Studio to run all objects in the "Q1" array through the getSummary script. This produces an error because the getSummary script references the sales key but there is no sales key in the third object.

The fix is to use hasKey in a conditional statement inside the getSummary script.

#define getSummary(object)

[[hasKey(object, 'sales') ? "[[object.branch]] achieved sales of [[abbreviateNumber(currencyFormat(object.sales))]]." : "There is no sales data for [[object.branch]]."]]

Now, [[forAll(WholeJSON.finances.Q1, object -> getSummary(object))]] generates a summary for each object.

Austin achieved sales of $2.5M.

Boston achieved sales of $3.5M.

There is no sales data for Dallas.

Denver achieved sales of $2.1M.

Tip

The conditional statement is written in ternary syntax. See Syntax for conditionals.