Logic Machine Forum
Json and data - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Json and data (/showthread.php?tid=2515)



Json and data - tomnord - 15.03.2020

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


RE: Json and data - benanderson_475 - 15.03.2020

(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)



RE: Json and data - tomnord - 17.03.2020

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-viruset/data/norway-table-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)


RE: Json and data - AlexLV - 17.03.2020

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??


RE: Json and data - Daniel - 17.03.2020

what FW do you use?


RE: Json and data - benanderson_475 - 17.03.2020

(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-viruset/data/norway-table-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)


RE: Json and data - AlexLV - 17.03.2020

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'


RE: Json and data - benanderson_475 - 17.03.2020

(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


RE: Json and data - Daniel - 17.03.2020

I tried this script and it works fine. Did you copy everything?


RE: Json and data - AlexLV - 18.03.2020

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)


RE: Json and data - Daniel - 18.03.2020

Maybe your device has no internet connection,


RE: Json and data - admin - 18.03.2020

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)