get_property

Modified on Thu, 9 Jul at 10:50 AM

get_property

TABLE OF CONTENTS

Get the value assigned to a key

Definition

any get_property(object $object, string $key)

Retrieves the value assigned to $key inside $object. Similar to using the dot accessor as in $object.$key, but allows variables as $key rather than just a static string. Returns null if $key doesn't exist in $object.

Parameters

object $object

Object inside which to look for the property.

string $key

Key inside $object to retrieve value of. Can be the result of an expression, see examples.

Returns

any

The value assigned to $key inside $object.

Examples

Sample data
{
  "firstName": "Jane",
  "lastName": "Doe",
  "age": 30,
  "status": {
    "Enabled": true
  },
  "roles": [
    "User",
    "Admin"
  ],
  "importantAttribute": "lastName"
}
Transformation
{
    name: get_property(@, 'firstName'),
    age: get_property(@, 'age'),
    enabled: get_property(get_property(@, 'status'), 'Enabled'),
    last_role: get_property(@, 'roles')[-1],
    lastName: get_property(@, get_property(@, 'importantAttribute')),      // impossible with . accessor
    middleName: get_property(@, 'middleName')
}
Result
{
  "name": "Jane",
  "age": 30,
  "enabled": true,
  "last_role": "Admin",
  "lastName": "Doe",
  "middleName": null
}

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article