Splitting a table in Lua

297 Views Asked by At

I have a script which gives me a table of timestamps and prices from a website API, I want to split them apart into timestamp and prices separately.

My code to get the prices and timestamps

local socket = require('socket.http')
local requests = require('requests')
local json = require('cjson')
local https = require('ssl.https')

local body, code, headers, status = https.request(URL)

if body then
print(body)
else
print(code)
end

Output:

{"success":true,"data":{"ee":[{"timestamp":1649192400,"price":55.0000},{"timestamp":1649196000,"price":60.0800},{"timestamp":1649199600,"price":60.0800},{"timestamp":1649203200,"price":99.7900},{"timestamp":1649206800,"price":99.7000},{"timestamp":1649210400,"price":104.9700},{"timestamp":1649214000,"price":118.4800},{"timestamp":1649217600,"price":165.6000},{"timestamp":1649221200,"price":177.8400},{"timestamp":1649224800,"price":179.6100},{"timestamp":1649228400,"price":159.3600},{"timestamp":1649232000,"price":100.0400},{"timestamp":1649235600,"price":77.0800},{"timestamp":1649239200,"price":91.5400},{"timestamp":1649242800,"price":97.2400},{"timestamp":1649246400,"price":93.9100},{"timestamp":1649250000,"price":92.9100},{"timestamp":1649253600,"price":99.4600},{"timestamp":1649257200,"price":126.2600},{"timestamp":1649260800,"price":165.5400},{"timestamp":1649264400,"price":178.4300},{"timestamp":1649268000,"price":171.0000},{"timestamp":1649271600,"price":132.4200},{"timestamp":1649275200,"price":109.1100},{"timestamp":1649278800,"price":98.6000}] 

And so on.

But I want it to be like:

Prices: 55.00, 60.80, and so on
Timestamps: 1649192400, 1649196000, and so on

How can I make it like that?


Edit: I have tried parsing JSON but all it did was print out:

Table: 0x559d70fd0ac0
1

There are 1 best solutions below

2
On BEST ANSWER

Because you printed the table, you need print the content of the table, something like:

local t = cjson.decode(body)
print('Prices:', t.data.ee[1].timestamp)