- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2022 12:32 PM
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",
}
}
]
}
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var parser = new JSONParser();
var parsed = parser.parse(responseBody);
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 | Information | Key is: 279 and value is: [object Object] | *** Script | Log 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.
Solved! Go to Solution.