Help with extracting and manipulating values in a json string

Hello,

I am getting the below string out of this query to google maps directions api. As you can see, I have “waypoints” and they will wary (in numbers) every time i make a new request:

The idea is to calculate the estimated time of arrival from each point to going through several waypoints.

the values required (and i am only interested in are in [legs][steps][duration][value]

start … to reach first point 46
to reach 2nd point 46 +10
to reach 3rd point 46 +10 + 36

and so on…

as i said, i will have from 1 to 25 waypoints to calculate…

Blockquote
https://maps.googleapis.com/maps/api/directions/json?destination=37.918183991823334,23.743770092845583&origin=37.918451700034275,23.714862200095485&waypoints=37.917540100000124,23.71288139999966|37.91540009999996,23.710926200000277|37.91344665740052,23.711367733777124|37.91243674092973,23.7135580927124&mode=driving&departure_time=now&traffic_model=best_guess&key=xxxxxxxxxxxxx

these are the results:

Blockquote
{
“geocoded_waypoints”:[
{
“geocoder_status”:“OK”,
“place_id”:“ChIJX03A0Cq8oRQRzFyFS_xKfmU”,
“types”:[
“premise”
]
},
{
“geocoder_status”:“OK”,
“place_id”:“ChIJ2SGjYiq8oRQRdxz6U1yuLZQ”,
“types”:[
“premise”
]
},
{
“geocoder_status”:“OK”,
“place_id”:“ChIJ_QQRmoG-oRQRP2X7zUC611E”,
“types”:[
“premise”
]
},
{
“geocoder_status”:“OK”,
“place_id”:“ChIJi_eeFYG-oRQR3BEMqDNktE0”,
“types”:[
“cafe”,
“establishment”,
“food”,
“point_of_interest”,
“restaurant”,
“store”
]
},
{
“geocoder_status”:“OK”,
“place_id”:“ChIJOV8fRRy_oRQRISoVLa7zfkk”,
“types”:[
“establishment”,
“laundry”,
“point_of_interest”
]
},
{
“geocoder_status”:“OK”,
“place_id”:“ChIJT8v50Cm9oRQRFxxppqpEdyk”,
“types”:[
“establishment”,
“point_of_interest”
]
}
],
“routes”:[
{
“bounds”:{
“northeast”:{
“lat”:37.926934,
“lng”:23.7442745
},
“southwest”:{
“lat”:37.912059,
“lng”:23.7099832
}
},
“copyrights”:“Map data ©2023”,
“legs”:[
{
“distance”:{
“text”:“0.2 km”,
“value”:237
},
“duration”:{
“text”:“1 min”,
“value”:46
},
“end_address”:“Riga Fereou 35, Alimos 174 55, Greece”,
“end_location”:{
“lat”:37.9175281,
“lng”:23.7129028
},
“start_address”:“Athinas 24, Alimos 174 55, Greece”,
“start_location”:{
“lat”:37.9184304,
“lng”:23.7148273
},
“steps”:[
{
“distance”:{
“text”:“84 m”,
“value”:84
},
“duration”:{
“text”:“1 min”,
“value”:10
},
“end_location”:{
“lat”:37.918653,
“lng”:23.713916
},
“html_instructions”:“Head west on Athinas toward Riga Fereou”,
“polyline”:{
“points”:“e}[email protected]@[email protected]@AF”
},
“start_location”:{
“lat”:37.9184304,
“lng”:23.7148273
},
“travel_mode”:“DRIVING”
},
{
“distance”:{
“text”:“0.2 km”,
“value”:153
},
“duration”:{
“text”:“1 min”,
“value”:36
},
“end_location”:{
“lat”:37.9175281,
“lng”:23.7129028
},
“html_instructions”:“Turn left at the 1st cross street onto Riga Fereou

Destination will be on the right
”,
“maneuver”:“turn-left”,
“polyline”:{
“points”:“q~lfF_svoCNN\X\ZZX\XHH\X\XRR”
},
“start_location”:{
“lat”:37.918653,
“lng”:23.713916
},
“travel_mode”:“DRIVING”
}
],
“traffic_speed_entry”:[
],
“via_waypoint”:[
]
},
{
“distance”:{
“text”:“0.3 km”,
“value”:293
},
“duration”:{
“text”:“1 min”,
“value”:67
},
“end_address”:“Riga Fereou 15a, Alimos 174 55, Greece”,
“end_location”:{
“lat”:37.9153872,
“lng”:23.7109489
},
“start_address”:“Riga Fereou 35, Alimos 174 55, Greece”,
“start_location”:{
“lat”:37.9175281,
“lng”:23.7129028
},
“steps”:[
{
“distance”:{
“text”:“0.3 km”,
“value”:293
},
“duration”:{
“text”:“1 min”,
“value”:67
},
“end_location”:{
“lat”:37.9153872,
“lng”:23.7109489
},
“html_instructions”:“Head southwest on Riga Fereou toward Aristoteli Valaoritou
Destination will be on the right
”,
“polyline”:{
“points”:“qwlfFslvoCFF\XBB\XVX@@\X\XZXRP\Z\XZX\XZXZX\XPP”
},
“start_location”:{
“lat”:37.9175281,
“lng”:23.7129028
},
“travel_mode”:“DRIVING”
}
],
“traffic_speed_entry”:[
],
“via_waypoint”:[
]
},
{
“distance”:{
“text”:“0.3 km”,
“value”:301
},
“duration”:{
“text”:“1 min”,
“value”:79
},
“end_address”:“Thoukididou 36, Alimos 174 55, Greece”,
“end_location”:{
“lat”:37.9134749,
“lng”:23.7113938
},
“start_address”:“Riga Fereou 15a, Alimos 174 55, Greece”,
“start_location”:{
“lat”:37.9153872,
“lng”:23.7109489
},
“steps”:[
{
“distance”:{
“text”:“0.1 km”,
“value”:145
},
“duration”:{
“text”:“1 min”,
“value”:32
},
“end_location”:{
“lat”:37.9143284,
“lng”:23.7099832
},
“html_instructions”:“Head southwest on Riga Fereou toward Themistokleous”,
“polyline”:{
“points”:“ejlfFmvoCHHPN\X\X\XFFZX\ZZZ" }, "start_location":{ "lat":37.9153872, "lng":23.7109489 }, "travel_mode":"DRIVING" }, { "distance":{ "text":"0.2 km", "value":156 }, "duration":{ "text":"1 min", "value":47 }, "end_location":{ "lat":37.9134749, "lng":23.7113938 }, "html_instructions":"Turn <b>left</b> onto <b>Thoukididou</b><div style="font-size:0.9em">Destination will be on the right</div>", "maneuver":"turn-left", "polyline":{ "points":"[email protected]@[email protected]@[email protected]@[email protected]" }, "start_location":{ "lat":37.9143284, "lng":23.7099832 }, "travel_mode":"DRIVING" } ], "traffic_speed_entry":[ ], "via_waypoint":[ ] }, { "distance":{ "text":"0.3 km", "value":276 }, "duration":{ "text":"1 min", "value":81 }, "end_address":"Leof. Kalamakiou 23, Alimos 174 55, Greece", "end_location":{ "lat":37.9124585, "lng":23.713528 }, "start_address":"Thoukididou 36, Alimos 174 55, Greece", "start_location":{ "lat":37.9134749, "lng":23.7113938 }, "steps":[ { "distance":{ "text":"0.2 km", "value":216 }, "duration":{ "text":"1 min", "value":66 }, "end_location":{ "lat":37.912059, "lng":23.7130626 }, "html_instructions":"Head <b>southeast</b> on <b>Thoukididou</b> toward <b>Metamorfoseos</b>", "polyline":{ "points":"e~kfFecvoCLYN[[email protected][email protected][LO^YLMPQX[LM" }, "start_location":{ "lat":37.9134749, "lng":23.7113938 }, "travel_mode":"DRIVING" }, { "distance":{ "text":"60 m", "value":60 }, "duration":{ "text":"1 min", "value":15 }, "end_location":{ "lat":37.9124585, "lng":23.713528 }, "html_instructions":"Turn <b>left</b> onto <b>Leof. Kalamakiou</b><div style="font-size:0.9em">Destination will be on the right</div>", "maneuver":"turn-left", "polyline":{ "points":"[email protected]][_@" }, "start_location":{ "lat":37.912059, "lng":23.7130626 }, "travel_mode":"DRIVING" } ], "traffic_speed_entry":[ ], "via_waypoint":[ ] }, { "distance":{ "text":"4.7 km", "value":4728 }, "duration":{ "text":"11 mins", "value":657 }, "end_address":"Koumoundourou 94, Alimos 174 56, Greece", "end_location":{ "lat":37.9181819, "lng":23.7437928 }, "start_address":"Leof. Kalamakiou 23, Alimos 174 55, Greece", "start_location":{ "lat":37.9124585, "lng":23.713528 }, "steps":[ { "distance":{ "text":"1.2 km", "value":1154 }, "duration":{ "text":"3 mins", "value":168 }, "end_location":{ "lat":37.9206308, "lng":23.7214174 }, "html_instructions":"Head <b>northeast</b> on <b>Leof. Kalamakiou</b> toward <b>Pizani</b>", "polyline":{ "points":"{[email protected][email protected]_@[email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected]][email protected][email protected][email protected][email protected]_@[]Y]CCY][email protected][]Y]OO[]WY[[[[EGUSSSY[WW[]W]KKY[[]IIY]MK]Y][email protected][[email protected]@O[[email protected][email protected]@[email protected]@[email protected]" }, "start_location":{ "lat":37.9124585, "lng":23.713528 }, "travel_mode":"DRIVING" }, { "distance":{ "text":"1.2 km", "value":1190 }, "duration":{ "text":"3 mins", "value":183 }, "end_location":{ "lat":37.9174744, "lng":23.7341716 }, "html_instructions":"Turn <b>right</b> onto <b>Theomitoros</b>", "maneuver":"turn-right", "polyline":{ "points":"}jmfF{[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@J[[email protected]@[email protected]@[email protected][email protected]][email protected][email protected]@@[email protected][[email protected][email protected]@[email protected]@[email protected]@[email protected]@?E?][email protected]@[email protected]@Ag@@[email protected]@[email protected]@DQBMJk@@[email protected]@D[@[email protected]@[email protected]@[email protected]@[email protected]@" }, "start_location":{ "lat":37.9206308, "lng":23.7214174 }, "travel_mode":"DRIVING" }, { "distance":{ "text":"1.1 km", "value":1086 }, "duration":{ "text":"3 mins", "value":155 }, "end_location":{ "lat":37.9252777, "lng":23.7413614 }, "html_instructions":"Turn <b>left</b> onto <b>Leof. Ionias</b>", "maneuver":"turn-left", "polyline":{ "points":"ewlfFqqzoCFQGGKK[][[[[[]QO[[[]CC[[][[[[[IIY]CC[[[[OO[][[[[[][[KK[[]YOO][email protected][email protected]][email protected][email protected][email protected][[email protected]]YECII]YMMSQII[[MMMMSSGG]YMK[[CE]YAA[Y]W]YOMMIUEAAOSGGAAOSKIQQII" }, "start_location":{ "lat":37.9174744, "lng":23.7341716 }, "travel_mode":"DRIVING" }, { "distance":{ "text":"0.3 km", "value":318 }, "duration":{ "text":"1 min", "value":44 }, "end_location":{ "lat":37.926902, "lng":23.7442686 }, "html_instructions":"Slight <b>right</b> to stay on <b>Leof. Ionias</b>", "maneuver":"turn-slight-right", "polyline":{ "points":"_hnfFo~{[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@" }, "start_location":{ "lat":37.9252777, "lng":23.7413614 }, "travel_mode":"DRIVING" }, { "distance":{ "text":"0.8 km", "value":786 }, "duration":{ "text":"1 min", "value":64 }, "end_location":{ "lat":37.9198365, "lng":23.7442694 }, "html_instructions":"Turn <b>right</b> onto <b>Leof. Vouliagmenis</b>", "maneuver":"turn-right", "polyline":{ "points":"crnfFup|oCV?b@?b@@b@?b@?b@?b@?b@?b@@b@?Z?b@?\[email protected]?b@?T?@?@?@?@?@@H?B?LAH?X?^?b@?b@?b@@b@?b@?b@?T?b@[email protected]@?X?@AN[email protected]@?@?" }, "start_location":{ "lat":37.926902, "lng":23.7442686 }, "travel_mode":"DRIVING" }, { "distance":{ "text":"49 m", "value":49 }, "duration":{ "text":"1 min", "value":12 }, "end_location":{ "lat":37.9194588, "lng":23.7441156 }, "html_instructions":"Turn <b>right</b> onto <b>Par. Leof. Vouliagmenis</b>", "maneuver":"turn-right", "polyline":{ "points":"_fmfFup|oC@[email protected]" }, "start_location":{ "lat":37.9198365, "lng":23.7442694 }, "travel_mode":"DRIVING" }, { "distance":{ "text":"0.1 km", "value":145 }, "duration":{ "text":"1 min", "value":31 }, "end_location":{ "lat":37.9181819, "lng":23.7437928 }, "html_instructions":"Keep <b>right</b> to stay on <b>Par. Leof. Vouliagmenis</b><div style="font-size:0.9em">Destination will be on the right</div>", "maneuver":"keep-right", "polyline":{ "points":"scmfFwo|oCV@\@[email protected]@[email protected]@J@[email protected]@[email protected]@" }, "start_location":{ "lat":37.9194588, "lng":23.7441156 }, "travel_mode":"DRIVING" } ], "traffic_speed_entry":[ ], "via_waypoint":[ ] } ], "overview_polyline":{ "points":"e}[email protected]@t@[email protected]@jB~A|[email protected]@gAlA}[email protected]@[email protected]@[email protected]}AwAmBeDoE_EkFkDgE{[email protected]@_FsB{@c@[email protected]@[email protected]@uCp@[email protected]@yBl@}BHy@@yAUsDBaCBc@PaAVwA@uBdAwFVuAR{@kAmA_E_EgDkDoBsBaB}[email protected]@[email protected]@yAaAkAaAgAeAcC_CyAmA{@[email protected][]_@[[[email protected]_AiBO][email protected]@[email protected][email protected]@[email protected]@[email protected][email protected]@[email protected]@NF@”
},
“summary”:“Athinas and Riga Fereou”,
“warnings”:[
],
“waypoint_order”:[
0,
1,
2,
3
]
}
],
“status”:“OK”
}
]

Thank you all in advance

Hi @xewonder, is this actually a string that still needs to be parsed or are you working with a proper JSON object?

I am not sure I fully understand your requirement, but based on your example it seems to me that you could do something like this:

This is the result for each leg:

That said, I do not think this is a great use case for n8n as you are performing calculations not natively supported in n8n and based on the index of each item. So for the first “step” in a “leg” you’d want to take no previous “steps” into account, but for the second “step” you’d want to take the one previous step into account and so on. That’s not something n8n does out of the box, so this is not very fun to set up.