Retrieve and save data from json api  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
steef27
Posts: 30
Joined: Friday 26 August 2016 9:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Retrieve and save data from json api

Post 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)
        
        ????????
        
Attachments
code.jpg
code.jpg (59.4 KiB) Viewed 1413 times
User avatar
waltervl
Posts: 5752
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Retrieve and save data from json api

Post by waltervl »

You better use the dzVents openURL function.
Check for example viewtopic.php?p=278473#p278473
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
steef27
Posts: 30
Joined: Friday 26 August 2016 9:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Retrieve and save data from json api

Post by steef27 »

ok, good suggestion!
How do I retrieve and save data behind "05. price"?
I am not very experienced with this format.
User avatar
waltervl
Posts: 5752
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

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

Post 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']
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
steef27
Posts: 30
Joined: Friday 26 August 2016 9:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Retrieve and save data from json api

Post by steef27 »

Thanks, that works perfect! :D
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest