Getting an Error Using Library: JSON Parser Tool

Using an IMP003, I am trying to use the JSON parser tool library but get the following error. “This might be a rookie mistake I am new to JSON”.

2016-06-19 00:35:27 UTC-4 [Agent] ERROR: the index 'slice' does not exist 2016-06-19 00:35:27 UTC-4 [Agent] ERROR: in parse ...imp#jsonparser.class.nut#1.0.0:285 2016-06-19 00:35:27 UTC-4 [Agent] ERROR: from getUbiDotsPwrBtn agent_code:505 2016-06-19 00:35:27 UTC-4 [Agent] ERROR: from main agent_code:823

This is the code I am using inside a function:
`#require "JSONParser.class.nut:1.0.0"
function getUbiDotsPwrBtn(){
local headers = { “Content-Type”: “GET”, “X-Auth-Token”: UBIDOTS_X_Auth_Token }; // Replace the token with yours

local url = “<>/values/?page_size=1&token=<>”;
local request = http.get(url, headers);
local response = request.sendsync();
result = JSONParser.parse(response);

The response being received is the following:
{ "count": 11, "next": "", "previous": null, "results": [ { "url": "", "value": 1.0, "timestamp": 1466309858069, "context": {}, "created_at": "2016-06-19T04:17:38.069" } ] }


request.sendsync() returns a table { statuscode, headers, body }. You need to check what’s in the table first (ie did the request complete successfully), then decode the JSON, which will be in response.body.

Generally, if you are decoding JSON in the agent, you don’t need the library, you can just use http.jsondecode(response.body)

PS. It’s always a good idea to wrap json decoding in try/catch as it will throw an exception if it has any trouble deciphering it.