JSON isn't parsed

(Maksim Storozhev) #1

Hello!

I’ve got JSON answer:

{
“results” : [
{
“geometry” : {
“location” : {
“lat” : 60.0357589,
“lng” : 30.4046353
},
“location_type” : “ROOFTOP”,
“viewport” : {
“northeast” : {
“lat” : 60.0371078802915,
“lng” : 30.4059842802915
},
“southwest” : {
“lat” : 60.03440991970849,
“lng” : 30.4032863197085
}
}
},
“place_id” : “ChIJpzGgYUMzlkYRqAqTZOGfcL8”,
“types” : [ “street_address” ]
}
],
“status” : “OK”
}

{{geocode_b.data__results}} works fine, but
{{geocode_b.data__results__types}} doesn’t work.

Why?

(John Jackson) #2

If you format the JSON, it looks like this:

{  
   "results":[  
      {  
         "geometry":{  
            "location":{  
               "lat":60.0357589,
               "lng":30.4046353
            },
            "location_type":"ROOFTOP",
            "viewport":{  
               "northeast":{  
                  "lat":60.0371078802915,
                  "lng":30.4059842802915
               },
               "southwest":{  
                  "lat":60.03440991970849,
                  "lng":30.4032863197085
               }
            }
         },
         "place_id":"ChIJpzGgYUMzlkYRqAqTZOGfcL8",
         "types":[  
            "street_address"
         ]
      }
   ],
   "status":"OK"
}

The [ means the data is in an array. You reference the first item of an array with __0__. The property results is an array, and the type itself is an array item.

So you should actually be using:

{{geocode_b.data__results__0__types__0}}

(Which means, look in results, find the first item of the array, find the property types within that, and then give me the value of the first array item within types).

(Maksim Storozhev) #3

Thank you for so complete answer. I really didn’t notice the bracket “[”. Of course, it’s array. Now I see. Sorry.

(Bryce Jurss) #4

Could you guys help me figure out how to parse this to access ‘lat’ output on bottom of JSON result. I have tried data__results__0__geometry__location__lat :
{
“results”: [
{
“address_components”: [
{
“long_name”: “1807”,
“short_name”: “1807”,
“types”: [
“street_number”
]
},
{
“long_name”: “South 11th Street”,
“short_name”: “S 11th St”,
“types”: [
“route”
]
},
{
“long_name”: “Waco”,
“short_name”: “Waco”,
“types”: [
“locality”,
“political”
]
},
{
“long_name”: “McLennan County”,
“short_name”: “McLennan County”,
“types”: [
“administrative_area_level_2”,
“political”
]
},
{
“long_name”: “Texas”,
“short_name”: “TX”,
“types”: [
“administrative_area_level_1”,
“political”
]
},
{
“long_name”: “United States”,
“short_name”: “US”,
“types”: [
“country”,
“political”
]
},
{
“long_name”: “76706”,
“short_name”: “76706”,
“types”: [
“postal_code”
]
}
],
“formatted_address”: “1807 S 11th St, Waco, TX 76706, USA”,
“geometry”: {
“location”: {
“lat”: 31.5386505,
“lng”: -97.1217719
},

(Bryce Jurss) #5

(Daniel Beckett) #6

The one you’re trying actually looks right if you take out the ‘data’ part (unless that’s what your webhook action is called?)

Try this:
results__0__geometry__location__lat

#7

Can you pass the value of an attribute as the index of an array rather than hardcoding it? ie {{geocode_b.data__[some attribute value]}} so it would return the array value for {{geocode_b.data__3}} if the attribute value was 3, etc. I’ve tried enclosing the attribute in double curly brackets but that hasn’t worked.

(Daniel Beckett) #9

@johnConnor

Unfortunately nested variables aren’t supported.

1 Like
#10

Hi @johnConnor,

after webhook and get attribute create a code action named Get Latitude

In code field try this:

var addresses = {{your_webhook_action_name.data__results}};
var index = {{your_trigger_action_name.attributes__addressindex}};

resolve(addresses[index].geometry.location.lat);

now you can access latitude with {{get_latitude.result}}

Nested Attributes in FlowXO?