This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Stucturing data
#1
Good evening. I'm reading the following data from an api. The timestamp is in UTC, and the table is not JSON.
Any ideas on how to make a table with the timestamp in localtime and date, and the data structured nicely?

Code:
   ["Value"]
    * string: [
  {
    "time": 1666717200,
    "price": 177.38
  },
  {
    "time": 1666720800,
    "price": 148.27
  },
  {
    "time": 1666724400,
    "price": 126.38
  },
  {
    "time": 1666728000,
    "price": 116.92
  },
  {
    "time": 1666731600,
    "price": 111.92
  },
  {
    "time": 1666735200,
    "price": 94.77
  },
Reply
#2
os.date() takes a timestamp as a second argument. This is what the API provides in "time" value.
Reply
#3
(31.10.2022, 14:36)admin Wrote: os.date() takes a timestamp as a second argument. This is what the API provides in "time" value.

Thank you, but what about the data, what is the best way to set this up as a table?
Reply
#4
This can be helpful: https://forum.logicmachine.net/showthread.php?tid=3391
Put data into storage and use .lp to display it.
Reply
#5
(31.10.2022, 14:56)admin Wrote: This can be helpful: https://forum.logicmachine.net/showthread.php?tid=3391
Put data into storage and use .lp to display it.

I think maybe I did not make my self understood correctly, sorry for that.
I would like to put this data in an LUA table sorted based on time. I cant seem to figure out how to take the json data and put in a lua table. Is that possible?
Reply
#6
The Value part looks like a JSON string. Have you tried decoding it?
Reply
#7
(01.11.2022, 08:48)admin Wrote: The Value part looks like a JSON string. Have you tried decoding it?

got it. thanx
Reply
#8
Code:
table.sort(spot, function(a, b)
  return a.price < b.price
end)

min = spot[1]
minvalue = min.price
minhour = min.time --:match('T(%d+)')

log(minvalue, minhour)

max = spot[#spot]
maxvalue = max.price
maxhour = max.time --:match('T(%d+)')

log(maxvalue, maxhour)
using this as I found on the forum and altered. How would you change so that "time" shows ad Date and Time?
Reply
#9
This will add a "datetime" key to each table element. You can change the format string as needed. See this for possible formats: https://man7.org/linux/man-pages/man3/strftime.3.html
Code:
for _, entry in ipairs(spot) do
  entry.datetime = os.date('%c', entry.time)
end
Reply
#10
thank you. and what would be the bast way to average these price values?
Reply
#11
Code:
sum = 0

for _, entry in ipairs(spot) do
  entry.datetime = os.date('%c', entry.time)
  sum = sum + entry.price
end

avg = sum / #spot
log(avg)
Reply


Forum Jump: