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.

Json and data
#1
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  Big Grin
Reply
#2
(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  Big Grin

Just use json lib t
hen 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)
Reply
#3
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)
Reply
#4
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??
Reply
#5
what FW do you use?
------------------------------
Ctrl+F5
Reply
#6
(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)
Reply
#7
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'
Reply
#8
(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
Reply
#9
I tried this script and it works fine. Did you copy everything?
------------------------------
Ctrl+F5
Reply
#10
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)
Reply
#11
Maybe your device has no internet connection,
------------------------------
Ctrl+F5
Reply
#12
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)
Reply


Forum Jump: