Logic Machine Forum
Stucturing data - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Stucturing data (/showthread.php?tid=4337)



Stucturing data - tomnord - 31.10.2022

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
  },



RE: Stucturing data - admin - 31.10.2022

os.date() takes a timestamp as a second argument. This is what the API provides in "time" value.


RE: Stucturing data - tomnord - 31.10.2022

(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?


RE: Stucturing data - admin - 31.10.2022

This can be helpful: https://forum.logicmachine.net/showthread.php?tid=3391
Put data into storage and use .lp to display it.


RE: Stucturing data - tomnord - 31.10.2022

(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?


RE: Stucturing data - admin - 01.11.2022

The Value part looks like a JSON string. Have you tried decoding it?


RE: Stucturing data - tomnord - 01.11.2022

(01.11.2022, 08:48)admin Wrote: The Value part looks like a JSON string. Have you tried decoding it?

got it. thanx


RE: Stucturing data - tomnord - 01.11.2022

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?


RE: Stucturing data - admin - 01.11.2022

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



RE: Stucturing data - tomnord - 01.11.2022

thank you. and what would be the bast way to average these price values?


RE: Stucturing data - admin - 01.11.2022

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)