Posts: 213
Threads: 62
Joined: May 2018
Reputation:
4
So, to pass time in an quarantine I'm looking into usefull and not so usefull scripts.
So far I've gotten som usefull stuff out of the way. Now for the fooling about scripts.
If I want to read data from a json table (if its called that), looking something like this:
{"confirmed":86,"dead":0,"recovered":0,"confirmedPer1kCapita":0.27991966956459474,"name":"Agder","countyCode":"42"},
How would I go about this?
I read the data from an url. I would like to get the numbers as local variables.
I'm sure it's an easy task, so just point me in the right direction
Posts: 139
Threads: 44
Joined: Dec 2017
Reputation:
4
(15.03.2020, 17:02)tomnord Wrote: So, to pass time in an quarantine I'm looking into usefull and not so usefull scripts.
So far I've gotten som usefull stuff out of the way. Now for the fooling about scripts.
If I want to read data from a json table (if its called that), looking something like this:
{"confirmed":86,"dead":0,"recovered":0,"confirmedPer1kCapita":0.27991966956459474,"name":"Agder","countyCode":"42"},
How would I go about this?
I read the data from an url. I would like to get the numbers as local variables.
I'm sure it's an easy task, so just point me in the right direction
Just use json lib then access all data value/s from the decoded json like this dec.countycode, dec.name etc
Code: require('json')
s = [[{"confirmed":86,"dead":0,"recovered":0,"confirmedPer1kCapita":0.27991966956459474,"name":"Agder","countyCode":"42"}]]
dec = json.decode(s)
log(dec.confirmed)
Posts: 213
Threads: 62
Joined: May 2018
Reputation:
4
17.03.2020, 10:10
(This post was last modified: 17.03.2020, 11:10 by tomnord.)
Thanks, so I've gotten a bit further, but not quite there.
I am now reading the decoded json from string, but now I'm stuck at setting up import from this URL:
https://www.vg.no/spesial/2020/corona-vi...-overview/
I'm guessing I need to shorten down the data from the URL.
Any tips?
got it:
require('ssl.https')
require('json')
data = ssl.https.request("https://www.vg.no/spesial/2020/corona-viruset/data/norway-table-overview/")
dec = json.decode(data)
confirmed= dec["cases"][4]["confirmed"]
log(confirmed)
Posts: 336
Threads: 76
Joined: Jun 2017
Reputation:
6
Hi, I just tried to repeat your script and see such error:
Resident script:4: bad argument #1 to 'decode' (string expected, got nil)
stack traceback:
[C]: in function 'decode'
What it could be??
Posts: 4654
Threads: 24
Joined: Aug 2017
Reputation:
208
what FW do you use?
------------------------------
Ctrl+F5
Posts: 139
Threads: 44
Joined: Dec 2017
Reputation:
4
17.03.2020, 19:31
(This post was last modified: 17.03.2020, 20:00 by benanderson_475.)
(17.03.2020, 10:10)tomnord Wrote: Thanks, so I've gotten a bit further, but not quite there.
I am now reading the decoded json from string, but now I'm stuck at setting up import from this URL:
https://www.vg.no/spesial/2020/corona-vi...-overview/
got it:
require('ssl.https')
require('json')
data = ssl.https.request("https://www.vg.no/spesial/2020/corona-viruset/data/norway-table-overview/")
dec = json.decode(data)
confirmed= dec["cases"][4]["confirmed"]
log(confirmed) try this,
Code: log(dec.totals.confirmed)
--instead of confirmed= dec["cases"][4]["confirmed"] use,
confirmed = dec.totals.confirmed
log(confirmed)
or log(dec.cases[4].confirmed)
Posts: 336
Threads: 76
Joined: Jun 2017
Reputation:
6
Daniel,
I am using latest FW 20191015 RC1 available.
I changed code as benanderson_475 advised, but anyway error in my error log:
Resident script:4: bad argument #1 to 'decode' (string expected, got nil)
stack traceback:
[C]: in function 'decode'
Posts: 139
Threads: 44
Joined: Dec 2017
Reputation:
4
(17.03.2020, 19:57)AlexLV Wrote: Daniel,
I am using latest FW 20191015 RC1 available.
I changed code as benanderson_475 advised, but anyway error in my error log:
Resident script:4: bad argument #1 to 'decode' (string expected, got nil)
stack traceback:
[C]: in function 'decode'
I think this means that it called the json.decode(data) function but no string was found (to be decoded) in the varible data, can you log the response data from the https get command and see what that returns, it may be empty which would explain the error
Posts: 4654
Threads: 24
Joined: Aug 2017
Reputation:
208
I tried this script and it works fine. Did you copy everything?
------------------------------
Ctrl+F5
Posts: 336
Threads: 76
Joined: Jun 2017
Reputation:
6
Daniel,
here I copied back from my LM:
require('ssl.https')
require('json')
data = ssl.https.request("https://www.vg.no/spesial/2020/corona-viruset/data/norway-table-overview/")
dec = json.decode(data)
confirmed = dec.totals.confirmed
log(confirmed)
Posts: 4654
Threads: 24
Joined: Aug 2017
Reputation:
208
Maybe your device has no internet connection,
------------------------------
Ctrl+F5
Posts: 7773
Threads: 42
Joined: Jun 2015
Reputation:
447
Logs the return values of https request:
Code: data, err = ssl.https.request("https://www.vg.no/spesial/2020/corona-viruset/data/norway-table-overview/")
log(data, err)
|