Page 1 of 1

Retrieve and save data from json api

Posted: Friday 01 October 2021 9:43
by steef27
Hi,

I would like to retrieve the price of a share every day and save it in a custom sensor.
I get the price with a json api call. I just can't manage to save the value after "02. open".

Does anyone have an idea?

Code: Select all

json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()  -- For Linux
        
-- API call
local config = assert(io.popen('curl "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol='..symbol..'&apikey='..apikey..'&datatype=json"'))
        local Stringjson = config:read('*all')
        --print(Stringjson)
        config:close()
        local jsonData = json:decode(Stringjson)
        
        ????????
        

Re: Retrieve and save data from json api

Posted: Friday 01 October 2021 12:22
by waltervl
You better use the dzVents openURL function.
Check for example viewtopic.php?p=278473#p278473

Re: Retrieve and save data from json api

Posted: Sunday 03 October 2021 11:13
by steef27
ok, good suggestion!
How do I retrieve and save data behind "05. price"?
I am not very experienced with this format.

Re: Retrieve and save data from json api  [Solved]

Posted: Sunday 03 October 2021 12:35
by waltervl
Should be something like below but it is not helping that the json field names have dots and spaces. So perhaps some special action is needed. I am also no expert.

Code: Select all

if (item.isJSON) then
					local result_table = item.json['Global Quote']
					local price = result_table['05. price']

Re: Retrieve and save data from json api

Posted: Sunday 03 October 2021 14:21
by steef27
Thanks, that works perfect! :D