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:
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
(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:
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:
12345678
require('json')
s = [[{"confirmed":86,"dead":0,"recovered":0,"confirmedPer1kCapita":0.27991966956459474,"name":"Agder","countyCode":"42"}]]dec = json.decode(s)
log(dec.confirmed)
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)
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:
12345
log(dec.totals.confirmed)
--instead of confirmed= dec["cases"][4]["confirmed"] use, confirmed = dec.totals.confirmedlog(confirmed)
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