JS Semantic error while accessing an object in a JSON array

Questions and answers about Plum Fuse+

Moderators: admin, support

  • Author
  • Message
Offline

Fivable

  • Posts: 10
  • Joined: Fri May 11, 2018 7:32 am

JS Semantic error while accessing an object in a JSON array

PostTue May 15, 2018 9:21 am

Hello,

Currently we are calling an endpoint in our own API to find a list of nearby locations to a particular device. The response will always be either an empty array or a JSON array with location objects. The example is a response containing only one location. Things seem to be working fine, and we use Fuse to make sure the array has a length, which also works, and if it is not empty, we need to read off the dropoff_description to the caller. This is where our problem comes in. We have tried a few different ways of getting to the object and the dropoff_description, including shifting the first object off the array and also just JS array syntax. With the REST module being called 'dropoffs', we have tried to read off dropoffs[0].dropoff_description without success. Regardless of how we try to access it, we get semantic errors.

It is also worth pointing out that I am assuming here that JSON responses are automatically parsed when the JSON result type is selected in the REST module. I've also tried manually JSON.parse(dropoffs) just in case, but this also results in a semantic error which is why I assume it has already been done. Perhaps there is something behind the scenes that I am not understanding, but any help would be appreciated.

Response
[
{
"id": 45,
"location_id": 45,
"code": "LN001",
"lot_name": "Fivable",
"dropoff_description": "Please return the Barnacle to the Fivable headquarters on the 2nd floor of the Parkview Building at 1204 Lexington Avenue in Irmo.",
"dropoff_address": "1204 Lexington Ave. 29063",
"lat": 34.085903,
"lng": -81.180025,
"data": "Some data",
"created_at": "2018-03-20 14:57:03",
"updated_at": "2018-03-20 14:57:03",
"deleted_at": null,
"distance": 375
}
]
Offline

support

  • Posts: 3517
  • Joined: Mon Jun 02, 2003 3:47 pm
  • Location: Boston, MA

Re: JS Semantic error while accessing an object in a JSON ar

PostTue May 15, 2018 3:13 pm

Please see the screenshot for how to access the "dropoff_description". You are correct, the JSON responses are automatically parsed.

https://drive.google.com/file/d/1GDVMYJ ... wlTLJ/view

As you can see, we use the shift() method. Theoretically, accessing dropoffs[0].dropoff_description should work too, but it hasn't in our testing. We believe this is a bug in Fuse+ and we are working on finding the root cause. Please use the shift() method in the meantime.
Offline

Fivable

  • Posts: 10
  • Joined: Fri May 11, 2018 7:32 am

Re: JS Semantic error while accessing an object in a JSON ar

PostTue May 15, 2018 3:30 pm

Thank you! I'm not sure what is different about what we tried when we attempted to use shift(), but this worked for us. Much appreciated.

Return to Plum Fuse+ Q&A

Who is online

Users browsing this forum: No registered users and 1 guest

cron