Get_property

This function can get a value of a data key.

Normally, the single dot “.” is used to access the values of objects (i.e. $object.$key). While this function can achieve the same result, it can also be used to get properties with variable names, since it accepts a string value.

Syntax

any get_property(json_object $object, string $key)

Outputs the value of defined $object.$key.

Parameters

$object: The object holding the property to get. Must be a JSON object.
$key: The key in the defined $object. Must be a string value.

Example

{
    "data": {
        "address1": "147 Columbia St",
        "address2": "541 Livingston St",
        "departments": [
            {
                "name": "Sales East",
                "location": "address1"
            },
            {
                "name": "Sales West",
                "location": "address2"
            }
        ]
    }
}

In this case the data has internal references. The “departments” reference other data points. To get the referenced values, get_property must be used.

{
    //These values represent the similarity between the dot and get_property
    sameResultDot: data.address1,
    sameResultGetProperty: get_property(data, address1),

    //These values represent the difference between the dot and get_property
    dotAccessorValue: data.departments[*].{ name: name,  location: location },
    getPropertyValue: data.departments[*].{ name: name,  location: get_property(parent(@), location) }
}

In this transformation, both the dot-accessor and the get_property functionality is used to illustrate the different results.

{
    "sameResultDot": "147 Columbia St",
    "sameResultGetProperty": "147 Columbia St",
    "dotAccessorValue": [
        {
            "name": "Sales East",
            "location": "address1"
        },
        {
            "name": "Sales West",
            "location": "address2"
        }
    ],
    "getPropertyValue": [
        {
            "name": "Sales East",
            "location": "147 Columbia St"
        },
        {
            "name": "Sales West",
            "location": "541 Livingston St"
        }
    ]
}

The “location” keys in the “dotAccessorValue” array are incorrect, as they display the reference key names, and not the value of the reference keys.

The “location” keys in the “getPropertyValue” array are correctly displaying the values of the reference keys.