Help Accessing Nested Object Attributes with TypeScript using VS Code

Hi Community,

I’m rather new to working with APIs and am trying to execute a call against the IdentitiesBetaApi, targeting the getIdentity method using Visual Studio Code. The call works as it should, and I am getting back the expected object from the service.

However, the specific attribute I need is nested within the parent object. As an example, I need to access the “attributes” nested object, and pull back specific attributes. Below is a snippet of what is returned from the call when executing it from Postman. You will see the nested “attributes” object in the json.

{
        "alias": "nobody",
        "emailAddress": "[email protected]",
        "isManager": false,
        "lastRefresh": "2025-05-15T17:46:40.821Z",
        "processingState": null,
        "identityStatus": "PENDING",
        "attributes": {
            "endDate": "1/1/1753 0:0:0 AM UTC",
            "jobTitle": "Newbie",
            "shift": "1",
            "cgnativeid": "NA"
            }
}

And below is my VS Code. The “val” object which I am returning at the end is being converted to a csv file in another function, and it lists all the attributes as headers, along with their values. One thing I noticed, was that for the attributes that exist within nested objects, the header would be “attributes.locationCode”, for example, while non nested attributes would just show as “firstname”.

I’ve been trying all sorts of things but no luck yet. Can someone point me to documentation or give me an example of how I can pull only the value for “attributes.locationCode”?

import { Configuration, IdentitiesBetaApi } from "sailpoint-api-client"

export const getIdentities = async() => {
    let apiConfig = new Configuration()
    let api = new IdentitiesBetaApi(apiConfig)

    let parameters = {
       
        id: 'someRandomID'
    }

    const val =  await api.getIdentity(parameters)

    return val

   
}

Thanks a lot!

Hello @kelvrobinson

Welcome to the Community :tada:
Try this :

import { Configuration, IdentitiesBetaApi } from "sailpoint-api-client"

export const getIdentities = async () => {
    let apiConfig = new Configuration()
    let api = new IdentitiesBetaApi(apiConfig)

    let parameters = {
        id: 'someRandomID'
    }

    try {
        const response = await api.getIdentity(parameters)

        // Assuming attributes.locationCode as required nested object
        const locationCode = response?.attributes?.locationCode

        return locationCode
    } catch (error) {
        console.error("Error fetching identity nested attribute:", error)
        return null
    }
}

Hi @sidharth_tarlapally - Thanks for the warm welcome and for the quick response.

I tried your revised code, below, but saw a red squiggly line under .attributes?, and the following error.

TSError: ⨯ Unable to compile TypeScript:
src/getIdentity.ts:27:35 - error TS2339: Property 'attributes' does not exist on type 'AxiosResponse<IdentityBeta, any>'.

So then I tried the following, which solved the issue with the property ‘attributes’ not existing, by accessing the ‘data’ object first.

try{
        const response = await api.getIdentity(parameters)

        const locationCodes = response?.data?.attributes?.locationCode
        //const locationCodes = response?.attributes?.locationCode

        //return barCode
        console.log(locationCodes)


    }

But this approach yielded a red squiggly line under ‘.locationCode’, and I received the following error.

TSError: ⨯ Unable to compile TypeScript:
src/getIdentity.ts:26:59 - error TS2339: Property 'locationCode' does not exist on type 'object'.

26         const locationCodes = response?.data?.attributes?.locationCode

I can confirm the attributes exist and are being returned in the Json. I think we’re headed down the right path but something is still off.

I’ve spoken with an architect at Sailpoint. I am using Node JS and typescript, so the path to accessing the data is slightly different.

The following returns what I am looking for..

try{
        const response = await api.getIdentity(parameters)

        const locationCodes = (response.data.attributes as any).locationCode

        console.log(locationCodes)
    }

Hope this helps anyone who runs into a similar issue!

1 Like