Iterate Through Array in JSON Object
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2018 09:31 AM
I am having problems with pulling out the data in the "addresses" array. Below is what my json looks like:
"{\"processing_time_milliseconds\": 714, \"addresses\": [{\"address1\": \"7899 hello road\", \"address2\": \"\", \"city\": \"Neverland\", \"state_or_province\": \"CA\", \"postal_code\": \"23487\", \"postal_code_extension\": \"3352\", \"county\": \"Land\", \"country_code\": \"US\", \"latitude\": \"22.43456\", \"longitude\": \"-45.67977\"}]}"
And here is the snipped of my code:
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var parser = new JSONParser();
var parsed = parser.parse(responseBody);
for (i = 0; i < parsed.addresses.length; i++) {
gs.print(parsed.addresses[i].address1);
}
It is never making into my for loop because the length is returning undefined. I am wanting to be able to parse out each individual piece of data to store other places (address, state, city, zip, etc.). Am I missing something here as to why this is not working?
Thanks!
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2018 09:39 AM
Works fine for me. You should print the responseBody to check, if you are getting expected values
*** Script: ++++length is++++++1
*** Script: 7899 hello road
var responseBody = "{\"processing_time_milliseconds\": 714, \"addresses\": [{\"address1\": \"7899 hello road\", \"address2\": \"\", \"city\": \"Neverland\", \"state_or_province\": \"CA\", \"postal_code\": \"23487\", \"postal_code_extension\": \"3352\", \"county\": \"Land\", \"country_code\": \"US\", \"latitude\": \"22.43456\", \"longitude\": \"-45.67977\"}]}";
var parser = new JSONParser();
var parsed = parser.parse(responseBody);
gs.print('++++length is++++++'+parsed.addresses.length);
for (i = 0; i < parsed.addresses.length; i++) {
gs.print(parsed.addresses[i].address1);
}
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2018 09:47 AM
So when I hardcode in the responseBody like you did it works fine, but if I leave it to where responseBody is filled with response.getBody() then it never works. It returns undefined.
Does it need to be encoded, decoded?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2018 09:57 AM
Ok...I think you need to stringify your response.
Try this
var responseBody = JSON.stringify(response.getBody());
var httpStatus = response.getStatusCode();
var parsed = JSON.parse(responseBody);
for (i = 0; i < parsed.addresses.length; i++) {
gs.print(parsed.addresses[i].address1);
}
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2018 10:03 AM
Yeah I thought the same thing too, but it still returns "undefined".