- 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-19-2022 06:32 PM - edited 12-19-2022 06:33 PM
Hi there,
The fact that you're running a try-catch and it is triggering the exception tells me there is some sort of issue with the JSON object being returned as the response body. Perhaps the size of the JSON string is too large as you have suggested which is causing it to be an invalid JSON object to try and parse.
Could you please log the ex.message to see more specifically what the exception/error is?
Regards,
Chris Perry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2022 09:27 AM
Thank you Chris; here is the error message:
} catch (ex) {
var message = ex.message;
gs.error("This is the ex error message" + message);
}
*** Script: This is the ex error messageCannot read property "first-name" from null: no thrown error
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2022 09:48 AM
@Enea1 There is one issue in object format.
The below marked object is present without key, thats wrong. Please check that.
Please mark as correct answer if this solves your issue.
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2022 10:01 AM
Hello Jaheer,
It's not, I assure you. The payload has 33k lines in it and when I first posted this question, I took one object from the array. In my inattention, I added an extra bracket that should not be there. The follow up replies address this. Apologies for the confusion. The payload is structured correctly and starts in this way:
{
"data": [
{
"id": "612",
"type": "projects",
"attributes": {
"object-type": "Project",
"team-id": 2,
"team": {
...
again, if I loop through and print only the user object, I get the object for all of them.. It is only a matter of the very last nested object inside user that is giving me problems.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2022 10:15 AM
@Enea1 Please try this
ServiceNow Community Rising Star, Class of 2023