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.

Extracting Values from JSON data - Amber Electric API
#2
Hi,

Try this:
Code:
local https = require('ssl.https')
local ltn12 = require('ltn12')
local json = require('json')
local cBody = '{"postcode": "3000"}'
local cReq = {}

result1 = https.request({
  url = 'https://api.amberelectric.com.au/prices/listprices',
  method = 'POST',
  headers = {
    ['content-length'] = #cBody,
    ['content-type'] = 'application/json'
  },
  source = ltn12.source.string(cBody),
  sink = ltn12.sink.table(cReq)
})

if (cReq) then
  mydata =  json.pdecode(table.concat(cReq))
  log(mydata)

  totalfixedKWHPrice = mydata.data.staticPrices.E1.totalfixedKWHPrice
  log(totalfixedKWHPrice)

  lossFactor = mydata.data.staticPrices.E1.lossFactor
  log(lossFactor)

  -- Retrieve the current price - the latest period with type ACTUAL
  wholesaleKWHPrice = mydata.data.variablePricesAndRenewables[#mydata.data.variablePricesAndRenewables].wholesaleKWHPrice
  log(wholesaleKWHPrice)

  currentPrice = totalfixedKWHPrice + lossFactor * wholesaleKWHPrice
  log(currentPrice)

end
#mydata.data.variablePricesAndRenewables gives you the length of the subset variablePricesAndRenewables and you pick up the latest field of the sub set variablePricesAndRenewables
Reply


Messages In This Thread
RE: Extracting Values from JSON data - Amber Electric API - by Erwin van der Zwart - 27.12.2020, 12:45

Forum Jump: