How to Remove Empty Arrays from Token Claims
Last Updated:
Overview
When there is an array that is applied as a custom attribute within the Application Profile and the value for that is empty, a Classic tenant will not add any claim to the token. However, on an Okta Identity Engine (OIE) org, the claim is not included if there were never any values included in the array. If any values are previously included in the attribute, an empty array will be included as a claim instead.
Applies To
- Okta Identity Engine (OIE)
- OIDC applications
Cause
This behavior occurs because Okta stores an empty array that has never had values as null and an empty array that previously had values as empty strings.
Solution
The solution to keeping consistency between users that never had values included in the attribute and users that previously had attributes in the claim is updating the mapping from the Okta User Profile to the Application User Profile and including the following expression language, replacing ATTRIBUTE_NAME with the name of the attribute itself.
Arrays.isEmpty(user.ATTRIBUTE_NAME) ? null : user.ATTRIBUTE_NAME
This expression will evaluate if the array user.ATTRIBUTE_NAME is empty. If empty, the value will be null; if not, it will apply this attribute name. On the token itself, this will result in no claim being added if there are no values in the array.
Using this expression will maintain consistency between Classic and OIE's behavior. It will also be consistent between users who never had values for the attribute and users who previously had values.
Below is a photo of what the mapping would look like when mapping a permissions attribute from the Okta User profile to an app_permissions attribute on the Application User Profile.
