Http.jsondecode and epoch time

I make a REST call and get back the following response body:
{“updated” : 1419634619222}

When I execute the following code:

local request = http.get(url, headers);
local response = request.sendsync();

data = http.jsondecode(response.body);
server.log(data.updated);

The following value is logged:
-1999555754

Why does http.jsondecode convert 1419634619222 to -1999555754?

What options are there to get the value back as: 1419634619222 ?

the value that you have is too large for 32 bit integers.

You should try to keep the values as floating point by adding decimals into your calculations. For example, look at this code

`_testvalue <- 1.41963 * (1000000000000.0);

server.log(_testvalue);

anewtable <- {testvalue = _testvalue, value32 = -1999555754, value_too_large = 1419634619222 };

server.log(http.jsonencode(anewtable));`

It will return:

[Agent] 1.41963e+12
[Agent] { “value32”: -1999555754, “testvalue”: 1.41963e+12, “value_too_large”: -1999555754 }

and so the ‘testvalue’ calculates properly as floating point but only if you don’t accidentally convert it to an integer. (add “.0” to the end of values)

I don’t know how many digits of precision are allowed or determined in floating point.

That is a 64-bit real time in milliseconds. There is no Squirrel datatype that can hold a 64-bit integer, so no function can return 1419634619222. However, if you don’t mind only getting the answer to the nearest second, and if you know that the answer is within about 2 million seconds (two billion milliseconds, or 49 days) of the current time, you can round it:

`local function round64bittime(t)
{
local now = time();
local t2 = now1000;
local diff = (t-t2)/1000;
if (diff > 2
10001000) {
diff -= 4294967; // this constant is (2**32)/1000
} else if (diff < -2
1000*1000) {
diff += 4294967;
}
return now + diff;
}

local body = “{“updated”: 1419634619222}”;
local data = http.jsondecode(body);
server.log(data.updated);
server.log(round64bittime(data.updated));`

Peter