- 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2022 03:00 PM - edited 12-20-2022 03:03 PM
I think I solved the issue. You cannot iterate through null values at all. The loop breaks the moment it hits a null object. You HAVE to check for null and skip the iteration using "continue". You also have to check for the string value of "null" and not just the keyword null.
This is working and providing me with the first name of all the users it iterates on:
try {
var r = new sn_ws.RESTMessageV2('STP CI Solicitation', 'Default GET');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var parser = new JSONParser();
var parsed = parser.parse(responseBody);
gs.info(httpStatus);
for (i = 0; i <= 10; i++) {
var userVariable = JSON.stringify((parsed.data[i]['attributes']['procurement-official']['user']));
if (userVariable != "null") {
gs.info(JSON.stringify((parsed.data[i]['attributes']['procurement-official']['user']['first-name'])));
} else {
continue;
}
}
} catch (ex) {
var message = ex.message;
gs.error("This is the ex error message" + message);
}
Thank you all for the help.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2022 01:19 PM - edited 12-15-2022 01:21 PM
Hi there,
Please try using below code:
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var responseBodyObj = JSON.parse(responseBody);
var dataArr = responseBodyObj.data;
for (var i = 0; i < dataArr.length; i++) {
gs.info(String(dataArr[i]['attributes']['procurement-official']['user']['first-name']));
}
Regards,
Chris Perry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2022 02:15 PM
Meaning like this. It wont work with either. Inside nor outside.
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var responseBodyObj = JSON.parse(responseBody);
var dataArr = responseBodyObj.data;
// var parser = new JSONParser();
// var parsed = parser.parse(responseBody);
// var objectType = "object-type";
// var createdAt = "created-at";
// var UpdatedAt = "updated-at";
// var purchasingAgency = "purchasing-agency";
// var archivedAt = "archived-at";
// var archivedBy = "archived-by";
// var procurementOfficial = "procurement-official";
// var firstName = "first-name";
// var lastName = "last-name";
// var solicitationType = "solicitations-type";
// var solicitationsVehicle = "solicitations-vehicle";
// var purchaseEstimate = "purchase-estimate";
// var solicitationsPurchaseClass = "solicitations-purchase-class";
// var solicitationsCategories = "solicitations-categories";
// var solicitationsSubCategories = "solicitations-subcategories";
gs.info(String(dataArr[2]['attributes']['procurement-official']['user']['first-name']));
// for (var i = 0; i < dataArr.length; i++) {
// gs.info(String(dataArr[i]['attributes']['procurement-official']['user']['first-name']));
// }

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2022 02:28 PM
Could you please provide the full JSON for the response w/ one object inside of the data property?
It's difficult to troubleshoot without the proper/complete JSON structure.
Regards,
Chris Perry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2022 02:51 PM
Like this, yea?
{
"data": [
{
{
"id": "1275",
"type": "projects",
"attributes": {
"object-type": "Project",
"team-id": 2,
"team": {
"id": 2,
"object-type": "Team",
"name": "Test name",
"created-at": "2019-12-17T05:11:41.711Z",
"updated-at": "2022-12-12T20:54:11.504Z"
},
"purchasing-agency-id": 203,
"purchasing-agency": {
"id": 203,
"team-id": 2,
"name": "Test Name of Agency",
"twitter-link": "https://twitter.com/dsfsdfsdf",
"facebook-link": null,
"youtube-link": "https://www.youtube.com/dsfsdfsdf",
"email": "mailto:info@dfsffsd.com",
"created-at": "2020-07-24T00:31:21.490Z",
"updated-at": "2022-09-01T23:57:23.015Z"
},
"name": "Name of project",
"status": "addendum",
"published-at": null,
"archived-at": null,
"procurement-official-id": 212,
"procurement-official": {
"id": 212,
"object-type": "Membership",
"team-id": 2,
"user-id": 135,
"user": {
"id": 135,
"first-name": "John",
"last-name": "Doe",
"email": "thebatman@yahoo.com",
"procurement-official-id": null,
"role": null
},
"created-at": "2020-09-17T01:02:56.140Z",
"updated-at": "2022-11-04T00:37:25.576Z"
},
"published-by-id": null,
"published-by": null,
"archived-by-id": null,
"archived-by": null,
"solicitations-type-id": 3,
"solicitations-type": {
"id": 3,
"object-type": "Solicitations::Type",
"team-id": 2,
"name": "Request For Offer",
"abbreviation": "RFO",
"show-lpa-options": true,
"created-at": "2020-01-17T17:34:12.668Z",
"updated-at": "2022-09-01T23:02:31.488Z"
},
"solicitations-vehicle-id": 3,
"solicitations-vehicle": {
"id": 3,
"object-type": "Solicitations::Vehicle",
"team-id": 2,
"name": "California Multiple Award Schedule",
"abbreviation": "CMAS",
"created-at": "2020-01-17T17:38:24.751Z",
"updated-at": "2022-12-12T20:28:32.127Z"
},
"purchase-estimate": "250000.0",
"solicitations-purchase-class-id": 4,
"solicitations-purchase-class": {
"id": 4,
"object-type": "Solicitations::PurchaseClass",
"team-id": 2,
"name": "IT Consulting Services",
"created-at": "2020-02-08T21:31:24.585Z",
"updated-at": "2022-12-12T20:53:49.791Z"
},
"master-document-id": 30867,
"solicitations-category-ids": [
5
],
"solicitations-categories": [
{
"id": 5,
"object-type": "Solicitations::Category",
"team-id": 2,
"name": "Consulting",
"created-at": "2020-03-03T20:59:56.895Z",
"updated-at": "2020-03-03T20:59:56.895Z"
}
],
"solicitations-subcategory-ids": [],
"solicitations-subcategories": [],
"external-id": "",
"created-at": "2022-07-05T21:09:04.900Z",
"updated-at": "2022-12-12T17:51:52.719Z"
}
}
]
}