Iterating through JSON arrays. Parsing Google Directions API


#1

1) Give a description of the problem
This has likely been done before but I wanted to give it a try for myself. I am trying to get an alert if my normal route to work is not the first suggested route by the Google Directions API.

Info here: https://developers.google.com/maps/documentation/directions/intro

My problems revolve around parsing arrays in JSON. The API response returns multiple routes with multiple steps and legs. I can parse the JSON using explicit indices but I need to be able to parse each route/leg/step to see if the expected set of directions appears (I haven’t fully thought out this logic yet). The number of routes seem to be constant but the number of steps and legs will vary.

Because of my inability to iterate through these arrays in the JSON I would have to hardcode everything and would have to make an assumption on the number of direction steps per route.

Questions:

  1. Is there a way to determine the length of an array in JSON? For example, length($response.routes). There can be multiple routes. If I had this value I could at least loop (I think) each element of the array.
  2. Is there a way to concatenate like attributes? For example, $response.routes[*].summary? This would give a list of all routes’ summary.
  3. Where do the results of “Parse JSON data” get stored? Does this deserialize the JSON?

Problem using size() to determine length of a JSON array
#2

$response.routes.length will give you the answer you seek

https://wiki.webcore.co/Functions#concat

Should be stored in the $json variable