Traffic time from Google API


#1

Hi folks, me again (trying to really dial in the morning routine info spoken from the Sonos. This time its travel time in traffic to a specific location.

Right now i’m starting basic using the info (and my own KEY) here:

As an example:
https://maps.googleapis.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA|via:Lexington,MA&key=YOUR_API_KEY

will return a host of JSON
specifically in my example specifying driving and with one waypoint to ensure the right route:

“legs” : [
{
“distance” : {
“text” : “6.8 mi”,
“value” : 10946
},
“duration” : {
“text” : “17 mins”,
“value” : 1041

can anyone with some JSON expertise help with the parse to JSON function when there are multiple values?

my ultimate goal would be to bring variables in and submit an expression URL made up of either my work, or my wife’s work location, and ideally the traffic in one hour from now (but need to cross this bridge first)


#2

after executing the parse JSON data variable, if you do log to console $json … makes it kind of easier to understand the variables.


#3

Also, look into the Google Distance Matrix API instead of the Directions API.


#4

cool, thanks - will try that :smiley:


#5

I was using the travel time from the distance matrix API i think - couldnt see a difference with my output


#6

The Distance Matrix is faster because it doesn’t have to generate the actual step by step directions behind the scenes. We make about 200,000 calls to it a day.


#7

yep you are right - the initial websites look very similar - great shout thanks


#8

already at the edge of my JSON knowledge here, so apologies for the extra question - i’m getting back
[:]
think i’m definitely missing something as the URL in a browser returns a clean:

{
“destination_addresses” : [ “XXXXXXX” ],
“origin_addresses” : [ “XXXXXX” ],
“rows” : [
{
“elements” : [
{
“distance” : {
“text” : “6.8 mi”,
“value” : 10946
},
“duration” : {
“text” : “17 mins”,
“value” : 1041
},
“status” : “OK”
}
]
}
],
“status” : “OK”
}


#9

share a green snapshot of the piston. mask anything private please.


#10

also, the quotes dont seem like normal double quotes which would look like this "

image


#11


#12

in the parse statement use the system variable $response instead of the local variable JSON_response

also insert a log to console $response between the get and the parse.


#13

cool - that works nicely - thank you - although now i’m back to my original problem of extracting the data from the formed JSON.

Can you suggest any good resources to ramp up on this (i’m also trying to work on a Office 365 graph for email and calendar - but its a pretty slow process - and authentication is a bridge too far right now)


#14

you are welcome. does the json parsing work … are you able to derive the variables from the parsed json?


#15

that’s the bit that still baffles me - i’m assuming i need to take the resulting JSON and form variables from specific portions/elements? Are you saying the parsing should add a selection of additional variables? If so i’m not able to find any.


#16

can you a masked version of the $json output that is logged?

easier to select and copy the text from the log, then mask it in the forum edit response window before posting.


#17

[status:OK, destination_addresses:[XXXX], origin_addresses:[XXXX], rows:[[elements:[[duration:[text:17 mins, value:1041], distance:[text:6.8 mi, value:10946], status:OK]]]]]

should $json and $response always show as ‘null’ - is a value only present for the period the piston runs?


#18

so you have variables like:

$json.status
$json.rows[0].elements[0].duration[0].text
$json.rows[0].elements[0].duration[0].value

and so on.

yes, they are only available at run time. save the $json to a local variable if you want to see it later.

EDIT: i could also be reading the map wrong … if it does not work using index, leave off the index and try.


#19

ok, thanks

So i have it clear

Get (URL) type JSON;
Do log info {$response};
do Parse JSON data {$json};

How do i set a local variable = $json.rows[0].elements[0].duration[0].text

I cant ammend the $json variable and the expression at time of editing is just reporting ‘null’

i think i could be just missing a simple UI component to either display or store the $json.rows[0]…


#20

define
string durationText
end define

get
parse JSON data $response
set variable durationText = $json.rows[0].elements[0].duration[0].text (this last part goes in the expression field)