Wyatt Patry
Posts: 0
Joined: Fri Jun 12, 2015 6:38 am

Passing JSON array to request parameter

Hi, I am trying to parse my array so I can format it for a request parameter that needs to be a linestring. I am getting json from the Google directions service, which returns a bunch of arrays, see sample here:

http://maps.googleapis.com/maps/api/d...

I need to pass the lng/lat from the steps array; start_location and end_location objects

I wrote this custom js before mapping the variables:

var obj = JSON.parse(localStorage.getItem("route_array"));
for (var i = 0, j = route_array.length; i < j; i++)
var lineString = obj.routes[1].legs[4].steps[4].start_location.lng + "," + obj.routes[1].leg[4].steps[4].start_location.lat +""+ obj.routes[1].legs[4].steps[2].end_location.lng +","+ obj.routes[1].legs[4].steps[2].end_location.lat +""+

localStorage.setItem("steps[lineString]");

So I need to repeat as many times as there are pairs of lat/lon generated, and place those into the linestring format, very simple: lng","lat""lng","lat etc... any help appreciated. When I try to run the this I keep getting undefined on legs, then steps etc... thanks!

Wyatt Patry
Posts: 0
Joined: Fri Jun 12, 2015 6:38 am

Passing JSON array to request parameter

I also tried to convert the directions service returned "overview_polyline" into a linestring but could not figure out how to properly load this library to run the conversion...

https://github.com/mapbox/polyline

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Passing JSON array to request parameter

Hello Wyatt,

Please use storage variable with type=array for this:
https://devcenter.appery.io/documenta...

Wyatt Patry
Posts: 0
Joined: Fri Jun 12, 2015 6:38 am

Passing JSON array to request parameter

Hi Sergiy, thank you, so many tutorials I overlooked the storage API summary...

So I was able to get the structure to pass to var steps properly but it shows null... it is passing "null,null null,null" now. I know my local storage variable 'route_array' is properly set because I can see it in the dev editor screen, it looks like it is in typical JSON...

Here is the new JS code I used:

var start_lng= localStorage.getItem("$['route_array'][0]['routes'][2]['legs'][6]['steps'][4]['lng']");
var start_lat= localStorage.getItem("$['route_array'][0]['routes'][2]['legs'][6]['steps'][4]['lat']");
var end_lng= localStorage.getItem("$['route_array'][0]['routes'][2]['legs'][6]['steps'][2]['lng']");
var end_lat= localStorage.getItem("$['route_array'][0]['routes'][2]['legs'][6]['steps'][2]['lat']");

for (var i = 0, j = localStorage.route_array.length; i < j; i++){
var lineString = start_lng +","+ start_lat +" "+ end_lng +","+ end_lat +"";}

Apperyio.storage.steps.set(lineString);

Am I not defining the var properly with the localStorage set? I tried using the Apperio.storage.storageVariable call to define but that threw errors.

Also, what is the proper place to run the script? Right now I have it as run JS before send. I notice that as soon as I load the page the var steps has already loaded "null,null null,null" so perhaps it has loaded too early? Should it only be on the var steps mapping?

Wyatt Patry
Posts: 0
Joined: Fri Jun 12, 2015 6:38 am

Passing JSON array to request parameter

Should I be using localStorage.getItem or apperyio.storage.route_array.get( to retrieve and define my var?

Wyatt Patry
Posts: 0
Joined: Fri Jun 12, 2015 6:38 am

Passing JSON array to request parameter

Thanks, I eventually figured it out with just mapping

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Passing JSON array to request parameter

It is incorrect to use such pathes (Mapping Expression) for localStorage. You should use them for storage variables: https://devcenter.appery.io/documenta...

So you need change:
var start_lng= localStorage.getItem("$['route_array'][0]['routes'][2]['legs'][6]['steps'][4]['lng']");

to:
var start_lng= Apperyio.storage.route_array.get("$[0]['routes'][2]['legs'][6]['steps'][4]['lng']");

Wyatt Patry
Posts: 0
Joined: Fri Jun 12, 2015 6:38 am

Passing JSON array to request parameter

Thank you! You guys are very responsive. I eventually figured it out by just mapping the array with an appropriate storage var model:

Image

So I did not require the JS afterall!

Return to “Issues”