Logic Machine Forum
JSON decode - 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: JSON decode (/showthread.php?tid=4861)



JSON decode - Joep - 05.07.2023

I got the JSON string below and like to decode it to date and type per item. How is this done best?

So for example: 2023-01-03 plastic

* string: {"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"}}


RE: JSON decode - Erwin van der Zwart - 06.07.2023

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



RE: JSON decode - Joep - 06.07.2023

(06.07.2023, 06:15)Erwin van der Zwart Wrote: 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

Thanks Erwin,
Regarding your examples i've added an extra example to get a correct notification at a matching day.

Code:
for key, value in pairs(data.ophaaldagen.data) do
  if value.date == os.date("%Y-%m-%d") then
    if value.nameType == "restafval" then
      log("grijze kliko")
    elseif value.nameType == "gft" then
      log("groene kliko")
    elseif value.nameType == "plastic" then
      log("plastic afval")
    end
  end
end



RE: JSON decode - Dré - 18.07.2023

Hi Joep,

Will you share how you did the rest done, like request the for the right postcode/place?


RE: JSON decode - Joep - 19.07.2023

(18.07.2023, 16:10)Dré Wrote: Hi Joep,

Will you share how you did the rest done, like request the for the right postcode/place?

Hi Dré,
First you need to check if this API is available for your address. See if you get info when adding your zipcode and house number to this url https://mijnafvalwijzer.nl/

If it works you can use the script below:

Code:
require('socket.http')
json = require('json')
socket.http.TIMEOUT = 5

data = socket.http.request('https://api.mijnafvalwijzer.nl/webservices/appsinput/?apikey=YOUR-API-KEY-HERE&method=postcodecheck&postcode=YOUR_ZIPCODE_HERE&street=&huisnummer=YOUR-HOUSE-NUMBER-HERE&toevoeging=&app_name=afvalwijzer&platform=phone&afvaldata=' .. os.date("%Y") .. '-01-01&langs=nl')

data = json.pdecode(data)

for key, value in pairs(data.ophaaldagen.data) do
  if value.date == os.date("%Y-%m-%d") then
    if value.nameType == "restafval" then
      log("grijze kliko")
    elseif value.nameType == "gft" then
      log("groene kliko")
    elseif value.nameType == "plastic" then
      log("plastic afval")
    end
  end
end