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.

JSON decode
#2
Hi Joep,

Try something like this:

Code:
require('json')
data = [[{"ophaaldagen":{"response":"OK","data":[{"nameType":"plastic","type":"plastic","date":"2023-01-03"},{"nameType":"gft","type":"gft","date":"2023-01-04"},{"nameType":"restafval","type":"restafval","date":"2023-01-11"},{"nameType":"plastic","type":"plastic","date":"2023-01-17"},{"nameType":"gft","type":"gft","date":"2023-01-18"},{"nameType":"gft","type":"gft","date":"2023-01-25"},{"nameType":"plastic","type":"plastic","date":"2023-01-31"},{"nameType":"gft","type":"gft","date":"2023-02-01"},{"nameType":"restafval","type":"restafval","date":"2023-02-08"},{"nameType":"plastic","type":"plastic","date":"2023-02-14"},{"nameType":"gft","type":"gft","date":"2023-02-15"},{"nameType":"gft","type":"gft","date":"2023-02-22"},{"nameType":"plastic","type":"plastic","date":"2023-02-28"},{"nameType":"gft","type":"gft","date":"2023-03-01"},{"nameType":"restafval","type":"restafval","date":"2023-03-08"},{"nameType":"plastic","type":"plastic","date":"2023-03-14"},{"nameType":"gft","type":"gft","date":"2023-03-15"},{"nameType":"gft","type":"gft","date":"2023-03-22"},{"nameType":"plastic","type":"plastic","date":"2023-03-28"},{"nameType":"gft","type":"gft","date":"2023-03-29"},{"nameType":"restafval","type":"restafval","date":"2023-04-05"},{"nameType":"plastic","type":"plastic","date":"2023-04-11"},{"nameType":"gft","type":"gft","date":"2023-04-12"},{"nameType":"gft","type":"gft","date":"2023-04-19"},{"nameType":"plastic","type":"plastic","date":"2023-04-25"},{"nameType":"gft","type":"gft","date":"2023-04-26"},{"nameType":"restafval","type":"restafval","date":"2023-05-03"},{"nameType":"plastic","type":"plastic","date":"2023-05-09"},{"nameType":"gft","type":"gft","date":"2023-05-10"},{"nameType":"gft","type":"gft","date":"2023-05-17"},{"nameType":"plastic","type":"plastic","date":"2023-05-23"},{"nameType":"gft","type":"gft","date":"2023-05-24"},{"nameType":"restafval","type":"restafval","date":"2023-05-31"},{"nameType":"plastic","type":"plastic","date":"2023-06-06"},{"nameType":"gft","type":"gft","date":"2023-06-07"},{"nameType":"gft","type":"gft","date":"2023-06-14"},{"nameType":"plastic","type":"plastic","date":"2023-06-20"},{"nameType":"gft","type":"gft","date":"2023-06-21"},{"nameType":"restafval","type":"restafval","date":"2023-06-28"},{"nameType":"plastic","type":"plastic","date":"2023-07-04"},{"nameType":"gft","type":"gft","date":"2023-07-05"},{"nameType":"gft","type":"gft","date":"2023-07-12"},{"nameType":"plastic","type":"plastic","date":"2023-07-18"},{"nameType":"gft","type":"gft","date":"2023-07-19"},{"nameType":"restafval","type":"restafval","date":"2023-07-26"},{"nameType":"plastic","type":"plastic","date":"2023-08-01"},{"nameType":"gft","type":"gft","date":"2023-08-02"},{"nameType":"gft","type":"gft","date":"2023-08-09"},{"nameType":"plastic","type":"plastic","date":"2023-08-15"},{"nameType":"gft","type":"gft","date":"2023-08-16"},{"nameType":"restafval","type":"restafval","date":"2023-08-23"},{"nameType":"plastic","type":"plastic","date":"2023-08-29"},{"nameType":"gft","type":"gft","date":"2023-08-30"},{"nameType":"gft","type":"gft","date":"2023-09-06"},{"nameType":"plastic","type":"plastic","date":"2023-09-12"},{"nameType":"gft","type":"gft","date":"2023-09-13"},{"nameType":"restafval","type":"restafval","date":"2023-09-20"},{"nameType":"plastic","type":"plastic","date":"2023-09-26"},{"nameType":"gft","type":"gft","date":"2023-09-27"},{"nameType":"gft","type":"gft","date":"2023-10-04"},{"nameType":"plastic","type":"plastic","date":"2023-10-10"},{"nameType":"gft","type":"gft","date":"2023-10-11"},{"nameType":"restafval","type":"restafval","date":"2023-10-18"},{"nameType":"plastic","type":"plastic","date":"2023-10-24"},{"nameType":"gft","type":"gft","date":"2023-10-25"},{"nameType":"gft","type":"gft","date":"2023-11-01"},{"nameType":"plastic","type":"plastic","date":"2023-11-07"},{"nameType":"gft","type":"gft","date":"2023-11-08"},{"nameType":"restafval","type":"restafval","date":"2023-11-15"},{"nameType":"plastic","type":"plastic","date":"2023-11-21"},{"nameType":"gft","type":"gft","date":"2023-11-22"},{"nameType":"gft","type":"gft","date":"2023-11-29"},{"nameType":"plastic","type":"plastic","date":"2023-12-05"},{"nameType":"gft","type":"gft","date":"2023-12-06"},{"nameType":"restafval","type":"restafval","date":"2023-12-13"},{"nameType":"plastic","type":"plastic","date":"2023-12-19"},{"nameType":"gft","type":"gft","date":"2023-12-20"},{"nameType":"gft","type":"gft","date":"2023-12-27"}],"error":false},"ophaaldagenNext":{"response":"NOK","data":[],"error":"No data"}}]]

luadata = json.pdecode(data)
sorteddata = {}

for key, value in pairs(luadata.ophaaldagen.data) do
  if sorteddata[value.nameType] == nil then
    sorteddata[value.nameType] = {}
  end
  table.insert(sorteddata[value.nameType], value.date)
end

for key, value in pairs(sorteddata) do
  table.sort(sorteddata[key], function(a, b) return a < b end)
end

log(sorteddata)

-- or something like this

sorteddata = {}

for key, value in pairs(luadata.ophaaldagen.data) do
  table.insert(sorteddata, {date = value.date, nameType = value.nameType} )
end

table.sort(sorteddata, function(a, b) return a['date'] < b['date'] end)

for key, value in pairs(sorteddata) do
  log( value.date .. " " .. value.nameType)
end
Reply


Messages In This Thread
JSON decode - by Joep - 05.07.2023, 15:28
RE: JSON decode - by Erwin van der Zwart - 06.07.2023, 06:15
RE: JSON decode - by Joep - 06.07.2023, 13:51
RE: JSON decode - by Dré - 18.07.2023, 16:10
RE: JSON decode - by Joep - 19.07.2023, 12:58

Forum Jump: