Not able to dot or bracket notation walk nested hyphenated JSON objects

Enea1
Tera Expert

Hello,

 

I am pulling JSON data from a third party API. The data object (although truncated to only show one), looks like this:

 

{
    "data": [
        {
            {
            "id": "625",
            "type": "projects",
            "attributes": {
                "object-type": "Project",
                "team-id": 2,
                "team": {
                    "id": 2,
                    "object-type": "Team",
                    "name": "Test team name",
                    "created-at": "2019-12-17T05:11:41.711Z",
                    "updated-at": "2022-12-12T20:54:11.504Z"
                },
                "purchasing-agency-id": 77,
                "purchasing-agency": {
                    "id": 77,
                    "team-id": 2,
                    "name": "Test Agency Name",
                    "twitter-link": "https://twitter.com/fsdfsdf",
                    "facebook-link": "https://www.facebook.com/sdfsdfsdfsdf",
                    "youtube-link": "https://www.youtube.com/cfdsfsfd",
                    "email": "mailto:webmaster@sdfdsfsfsfsdfs.com",
                    "created-at": "2020-07-24T00:31:20.363Z",
                    "updated-at": "2022-09-01T23:57:21.694Z"
                },
                "name": "Test software name",
                "status": "archived",
                "published-at": null,
                "archived-at": "2021-01-26T18:56:27.496Z",
                "procurement-official-id": 214,
                "procurement-official": {
                    "id": 214,
                    "object-type": "Membership",
                    "team-id": 2,
                    "user-id": 154,
                    "user": {
                        "id": 154,
                        "first-name": "Jim",
                        "last-name": "Hanson",
                        "email": "jim.hanson@yahoo.com",
                    }
        }
    ]
}

 

 
 
I am trying to set up a scheduled job script that will pull this data and update records on my instance every day.
 
To test the data, I am trying to access for example, the first name of the individual. 
After setting the following variables:

 

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var parser = new JSONParser();
var parsed = parser.parse(responseBody);

 

 
This works and I get a name showing in the logs:

 

​gs.log(parsed.data[1]['attributes']['procurement-official']['user']['first-name']


however, the moment I put it inside any sort of loop, it breaks down and no longer returns what I need. The log is just filled with system logs that are difficult to parse through but they don't even seem related.

 


I have tried with and without quotation marks along with every combination of dot/bracket walking possible:

 

for (var key in parsed.data) {
       gs.info("Key is: " + key + " and value is: " + parsed.data[key]['attributes']['procurement-official']['user']['first-name']);
}

 

...

 

​for (i = 0; i < parsed.data.length; i++) {
gs.log(parsed.data[i]['attributes']['procurement-official']['user']['first-name']);
}


For some reason, if I log everything up to

['user']

 I do get: 

 

 

2022-12-15 11:57:01
InformationKey is: 279 and value is: [object Object]*** ScriptLog Entry

 

So it's picking up that an object exists.. it just wont let me go into the last key of that object.

Again, outside of the loop and targeting one object, the notation seems to work.

 

Any ideas what I'm doing wrong with the loop?

 

Thank you in advance.