Extract values from json format

Post your feature requets for new triggers, conditions, actions and other improvements.

Moderator: Martin

Locked
davinik
Posts: 11
Joined: 23 Oct 2018 12:36

Extract values from json format

Post by davinik » 18 Nov 2018 21:28

I'd like to extract some values from Json format:
The file contain this values

{"latitude":45.89,"longitude":12.3,"timezone":"Europe/Rome","currently":{"time":1542528923,"summary":"Clear","icon":"clear-day","precipIntensity":0,"precipProbability":0,"temperature":5.34,"apparentTemperature":5.34,"dewPoint":-1.62,"humidity":0.61,"pressure":1023.79,"windSpeed":0.98,"windGust":2.25,"windBearing":24,"cloudCover":0,"uvIndex":0,"visibility":13.76,"ozone":332.58},"offset":1}

I d like to extract temperate humidity.. Etc

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Extract values from json format

Post by Desmanto » 19 Nov 2018 01:59

Use fromJSON() to convert the json.

Code: Select all

res = '{"latitude":45.89,"longitude":12.3,"timezone":"Europe/Rome","currently":{"time":1542528923,"summary":"Clear","icon":"clear-day","precipIntensity":0,"precipProbability":0,"temperature":5.34,"apparentTemperature":5.34,"dewPoint":-1.62,"humidity":0.61,"pressure":1023.79,"windSpeed":0.98,"windGust":2.25,"windBearing":24,"cloudCover":0,"uvIndex":0,"visibility":13.76,"ozone":332.58},"offset":1}';
js = fromJSON(res);
{js} is now a nested map-list object. Access temperature by

Code: Select all

temp = js["currently"]["temperature"]; // 5.34
To access humidity,

Code: Select all

temp = js["currently"]["humidity"]; // 0.61
Add condition debug dialog after the script to check the value of js and use the one you need.

You posted a related question about using location variable in HTTP request. There is a similar question already before and cover both (location and json). There is also example flow you can modify to suit your need : viewtopic.php?f=5&t=7457
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

davinik
Posts: 11
Joined: 23 Oct 2018 12:36

Re: Extract values from json format

Post by davinik » 23 Nov 2018 08:35

Thank you very much.. now another question:
from same list i'd like to access the list ot temperatures of the day

{"latitude":45.89,"longitude":12.3,"timezone":"Europe/Rome","currently":
{"time":1542957662,...,"temperature":6.91,...},
"hourly":{"summary":"Rain starting this evening.","icon":"rain","data":[
{"time":1542956400,...,"temperature":6.68,"apparentTemperatu....1},
{"time":1542960000,...,"temperature":7.34,"apparentTemperature":7.34,..},
{"time":1542963600,...,"temperature":8.08,"

I'd like to make a window dialog with temperatures : [ 7, 7, 8 ]

davinik
Posts: 11
Joined: 23 Oct 2018 12:36

Re: Extract values from json format

Post by davinik » 28 Nov 2018 09:11

SOLUTION:

js = fromJSON(weatherDark);

RainDa1 = js["currently"]["precipProbability"];
RainDa="{RainDa1*100,numberformat,0}";
TempDa1 = js["currently"] ["temperature"] ;
TempDa ="{TempDa1,numberformat,0}";
HuDa1 = js["currently"] ["humidity"]; // 0.61
HuDa ="{HuDa1*100,numberformat,0}";
StatusDa = js["currently"] ["summary"];
HourlyDa = js["hourly"] ["summary"];

//Array temp e precip [mm] giornaliere
DataArr = js["hourly"]["data"];
TemG=newList() ;
PrecG=newList() ;
TempL=newList();
TempL= [1 to 36];
a=1;
for(i in TempL)
{
TempI=(DataArr["temperature"]);
PrecIn=(DataArr["precipIntensity"]);
TempIc ="{TempI,numberformat,0}";
PrecInc="{PrecIn,numberformat,0}";

if ((i==a))
{
addElement(TemG,TempIc);
addElement(PrecG,PrecInc);
a=a+4;
}
}

Locked