Access Address Components keys/values from Google Maps API results

Hi there!

I have a HTTP request Node which queries Google Maps API and returns address data (example output below). I need to access the address_components fields so I can structure the address correctly in a database.

How can I run through this data, and output the long_names as values with the ‘types’ as the keys?

I’ve found a few scripts online that appear to do something similar (How to parse google maps Address components. (geocoder response) | by Ahmed Ali Thabet | Medium) and don’t think it can be that difficult but can’t for the life of me get them to work when I paste into a Function Node and attempt to access via items. Can anyone please help me?

[{
  "plus_code": {
    "compound_code": "Q2G3+XXP Selby, UK",
    "global_code": "9C5XQ2G3+XXP"
  },
  "results": [{
    "address_components": [{
      "long_name": "5",
      "short_name": "5",
      "types": [
        "street_number"
      ]
    },
    {
      "long_name": "The Shrubberies",
      "short_name": "The Shrubberies",
      "types": [
        "route"
      ]
    },
    {
      "long_name": "Cliffe",
      "short_name": "Cliffe",
      "types": [
        "locality",
        "political"
      ]
    },
    {
      "long_name": "Selby",
      "short_name": "Selby",
      "types": [
        "postal_town"
      ]
    },
    {
      "long_name": "North Yorkshire",
      "short_name": "North Yorkshire",
      "types": [
        "administrative_area_level_2",
        "political"
      ]
    },
    {
      "long_name": "England",
      "short_name": "England",
      "types": [
        "administrative_area_level_1",
        "political"
      ]
    },
    {
      "long_name": "United Kingdom",
      "short_name": "GB",
      "types": [
        "country",
        "political"
      ]
    },
    {
      "long_name": "YO8 6PW",
      "short_name": "YO8 6PW",
      "types": [
        "postal_code"
      ]
    }
  ],
  "formatted_address": "5 The Shrubberies, Cliffe, Selby YO8 6PW, UK",
  "geometry": {
    "bounds": {
      "northeast": {
        "lat": 53.7776495,
        "lng": -0.9949507999999999
      },
      "southwest": {
        "lat": 53.7775064,
        "lng": -0.9951573
      }
    },
    "location": {
      "lat": 53.7775733,
      "lng": -0.9950642999999999
    },
    "location_type": "ROOFTOP",
    "viewport": {
      "northeast": {
        "lat": 53.7789269302915,
        "lng": -0.9937050697084978
      },
      "southwest": {
        "lat": 53.7762289697085,
        "lng": -0.9964030302915019
      }
    }
  },
  "place_id": "ChIJ52OBwh0jeUgRAaAPAnafIZY",
  "types": [
    "premise"
  ]
},
{
  "address_components": [{
    "long_name": "8",
    "short_name": "8",
    "types": [
      "street_number"
    ]
  },
  {
    "long_name": "The Shrubberies",
    "short_name": "The Shrubberies",
    "types": [
      "route"
    ]
  },
  {
    "long_name": "Cliffe",
    "short_name": "Cliffe",
    "types": [
      "locality",
      "political"
    ]
  },
  {
    "long_name": "Selby",
    "short_name": "Selby",
    "types": [
      "postal_town"
    ]
  },
  {
    "long_name": "North Yorkshire",
    "short_name": "North Yorkshire",
    "types": [
      "administrative_area_level_2",
      "political"
    ]
  },
  {
    "long_name": "England",
    "short_name": "England",
    "types": [
      "administrative_area_level_1",
      "political"
    ]
  },
  {
    "long_name": "United Kingdom",
    "short_name": "GB",
    "types": [
      "country",
      "political"
    ]
  },
  {
    "long_name": "YO8 6PW",
    "short_name": "YO8 6PW",
    "types": [
      "postal_code"
    ]
  }
],
"formatted_address": "8 The Shrubberies, Cliffe, Selby YO8 6PW, UK",
"geometry": {
  "location": {
    "lat": 53.7775616,
    "lng": -0.9949786
  },
  "location_type": "ROOFTOP",
  "viewport": {
    "northeast": {
      "lat": 53.7789105802915,
      "lng": -0.993629619708498
    },
    "southwest": {
      "lat": 53.7762126197085,
      "lng": -0.996327580291502
    }
  }
},
"place_id": "ChIJV9Be2h0jeUgRZMzUX0q07ME",
"plus_code": {
  "compound_code": "Q2H4+22 Selby, UK",
  "global_code": "9C5XQ2H4+22"
},
"types": [
  "street_address"
]
},
{
  "address_components": [{
    "long_name": "The Shrubberies",
    "short_name": "The Shrubberies",
    "types": [
      "establishment",
      "point_of_interest",
      "transit_station"
    ]
  },
  {
    "long_name": "Cliffe",
    "short_name": "Cliffe",
    "types": [
      "locality",
      "political"
    ]
  },
  {
    "long_name": "Selby",
    "short_name": "Selby",
    "types": [
      "postal_town"
    ]
  },
  {
    "long_name": "North Yorkshire",
    "short_name": "North Yorkshire",
    "types": [
      "administrative_area_level_2",
      "political"
    ]
  },
  {
    "long_name": "England",
    "short_name": "England",
    "types": [
      "administrative_area_level_1",
      "political"
    ]
  },
  {
    "long_name": "United Kingdom",
    "short_name": "GB",
    "types": [
      "country",
      "political"
    ]
  },
  {
    "long_name": "YO8 6NH",
    "short_name": "YO8 6NH",
    "types": [
      "postal_code"
    ]
  }
],
"formatted_address": "The Shrubberies, Cliffe, Selby YO8 6NH, UK",
"geometry": {
  "location": {
    "lat": 53.777054,
    "lng": -0.994312
  },
  "location_type": "GEOMETRIC_CENTER",
  "viewport": {
    "northeast": {
      "lat": 53.7784029802915,
      "lng": -0.9929630197084979
    },
    "southwest": {
      "lat": 53.7757050197085,
      "lng": -0.995660980291502
    }
  }
},
"place_id": "ChIJzx4gkx0jeUgRfAYg3nLwVJc",
"plus_code": {
  "compound_code": "Q2G4+R7 Selby, UK",
  "global_code": "9C5XQ2G4+R7"
},
"types": [
  "establishment",
  "point_of_interest",
  "transit_station"
]
},
{
  "address_components": [{
    "long_name": "Q2G3+XX",
    "short_name": "Q2G3+XX",
    "types": [
      "plus_code"
    ]
  },
  {
    "long_name": "Selby",
    "short_name": "Selby",
    "types": [
      "postal_town"
    ]
  },
  {
    "long_name": "England",
    "short_name": "England",
    "types": [
      "administrative_area_level_1",
      "political"
    ]
  },
  {
    "long_name": "United Kingdom",
    "short_name": "GB",
    "types": [
      "country",
      "political"
    ]
  }
],
"formatted_address": "Q2G3+XX Selby, UK",
"geometry": {
  "bounds": {
    "northeast": {
      "lat": 53.7775,
      "lng": -0.9949999999999999
    },
    "southwest": {
      "lat": 53.777375,
      "lng": -0.995125
    }
  },
  "location": {
    "lat": 53.77745900000001,
    "lng": -0.995035
  },
  "location_type": "GEOMETRIC_CENTER",
  "viewport": {
    "northeast": {
      "lat": 53.77878648029149,
      "lng": -0.993713519708498
    },
    "southwest": {
      "lat": 53.77608851970849,
      "lng": -0.9964114802915021
    }
  }
},
"place_id": "GhIJjn3JxoPjSkAR8wLso1PX778",
"plus_code": {
  "compound_code": "Q2G3+XX Selby, UK",
  "global_code": "9C5XQ2G3+XX"
},
"types": [
  "plus_code"
]
},
{
  "address_components": [{
    "long_name": "1-38",
    "short_name": "1-38",
    "types": [
      "street_number"
    ]
  },
  {
    "long_name": "The Shrubberies",
    "short_name": "The Shrubberies",
    "types": [
      "route"
    ]
  },
  {
    "long_name": "Cliffe",
    "short_name": "Cliffe",
    "types": [
      "locality",
      "political"
    ]
  },
  {
    "long_name": "Selby",
    "short_name": "Selby",
    "types": [
      "postal_town"
    ]
  },
  {
    "long_name": "North Yorkshire",
    "short_name": "North Yorkshire",
    "types": [
      "administrative_area_level_2",
      "political"
    ]
  },
  {
    "long_name": "England",
    "short_name": "England",
    "types": [
      "administrative_area_level_1",
      "political"
    ]
  },
  {
    "long_name": "United Kingdom",
    "short_name": "GB",
    "types": [
      "country",
      "political"
    ]
  },
  {
    "long_name": "YO8",
    "short_name": "YO8",
    "types": [
      "postal_code",
      "postal_code_prefix"
    ]
  }
],
"formatted_address": "1-38 The Shrubberies, Cliffe, Selby YO8, UK",
"geometry": {
  "bounds": {
    "northeast": {
      "lat": 53.77829819999999,
      "lng": -0.9947021999999999
    },
    "southwest": {
      "lat": 53.7770666,
      "lng": -0.9961247000000001
    }
  },
  "location": {
    "lat": 53.7780139,
    "lng": -0.9948007
  },
  "location_type": "GEOMETRIC_CENTER",
  "viewport": {
    "northeast": {
      "lat": 53.7790313802915,
      "lng": -0.9940644697084979
    },
    "southwest": {
      "lat": 53.7763334197085,
      "lng": -0.996762430291502
    }
  }
},
"place_id": "ChIJK6MF3R0jeUgRgBN8BloTElQ",
"types": [
  "route"
]
},
{
  "address_components": [{
    "long_name": "YO8 6PW",
    "short_name": "YO8 6PW",
    "types": [
      "postal_code"
    ]
  },
  {
    "long_name": "The Shrubberies",
    "short_name": "The Shrubberies",
    "types": [
      "route"
    ]
  },
  {
    "long_name": "Cliffe",
    "short_name": "Cliffe",
    "types": [
      "locality",
      "political"
    ]
  },
  {
    "long_name": "Selby",
    "short_name": "Selby",
    "types": [
      "postal_town"
    ]
  },
  {
    "long_name": "North Yorkshire",
    "short_name": "North Yorkshire",
    "types": [
      "administrative_area_level_2",
      "political"
    ]
  },
  {
    "long_name": "England",
    "short_name": "England",
    "types": [
      "administrative_area_level_1",
      "political"
    ]
  },
  {
    "long_name": "United Kingdom",
    "short_name": "GB",
    "types": [
      "country",
      "political"
    ]
  }
],
"formatted_address": "The Shrubberies, Cliffe, Selby YO8 6PW, UK",
"geometry": {
  "bounds": {
    "northeast": {
      "lat": 53.7800042,
      "lng": -0.9946520999999999
    },
    "southwest": {
      "lat": 53.7766193,
      "lng": -0.9963685999999999
    }
  },
  "location": {
    "lat": 53.7781428,
    "lng": -0.9954843000000001
  },
  "location_type": "APPROXIMATE",
  "viewport": {
    "northeast": {
      "lat": 53.7800042,
      "lng": -0.9941613697084978
    },
    "southwest": {
      "lat": 53.7766193,
      "lng": -0.9968593302915019
    }
  }
},
"place_id": "ChIJrRz22h0jeUgRbnV5W211As4",
"types": [
  "postal_code"
]
},
{
  "address_components": [{
    "long_name": "Cliffe",
    "short_name": "Cliffe",
    "types": [
      "locality",
      "political"
    ]
  },
  {
    "long_name": "Selby",
    "short_name": "Selby",
    "types": [
      "postal_town"
    ]
  },
  {
    "long_name": "North Yorkshire",
    "short_name": "North Yorkshire",
    "types": [
      "administrative_area_level_2",
      "political"
    ]
  },
  {
    "long_name": "England",
    "short_name": "England",
    "types": [
      "administrative_area_level_1",
      "political"
    ]
  },
  {
    "long_name": "United Kingdom",
    "short_name": "GB",
    "types": [
      "country",
      "political"
    ]
  },
  {
    "long_name": "YO8",
    "short_name": "YO8",
    "types": [
      "postal_code",
      "postal_code_prefix"
    ]
  }
],
"formatted_address": "Cliffe, Selby YO8, UK",
"geometry": {
  "bounds": {
    "northeast": {
      "lat": 53.7899617,
      "lng": -0.9883202
    },
    "southwest": {
      "lat": 53.774036,
      "lng": -1.0044702
    }
  },
  "location": {
    "lat": 53.7800142,
    "lng": -0.9971095000000001
  },
  "location_type": "APPROXIMATE",
  "viewport": {
    "northeast": {
      "lat": 53.7899617,
      "lng": -0.9883202
    },
    "southwest": {
      "lat": 53.774036,
      "lng": -1.0044702
    }
  }
},
"place_id": "ChIJ_5S7aicjeUgRtMVpoiaR8qE",
"types": [
  "locality",
  "political"
]
},
{
  "address_components": [{
    "long_name": "Cliffe",
    "short_name": "Cliffe",
    "types": [
      "administrative_area_level_4",
      "political"
    ]
  },
  {
    "long_name": "Selby",
    "short_name": "Selby",
    "types": [
      "postal_town"
    ]
  },
  {
    "long_name": "North Yorkshire",
    "short_name": "North Yorkshire",
    "types": [
      "administrative_area_level_2",
      "political"
    ]
  },
  {
    "long_name": "England",
    "short_name": "England",
    "types": [
      "administrative_area_level_1",
      "political"
    ]
  },
  {
    "long_name": "United Kingdom",
    "short_name": "GB",
    "types": [
      "country",
      "political"
    ]
  },
  {
    "long_name": "YO8",
    "short_name": "YO8",
    "types": [
      "postal_code",
      "postal_code_prefix"
    ]
  }
],
"formatted_address": "Cliffe, Selby YO8, UK",
"geometry": {
  "bounds": {
    "northeast": {
      "lat": 53.8218872,
      "lng": -0.9341634999999999
    },
    "southwest": {
      "lat": 53.7598041,
      "lng": -1.0365264
    }
  },
  "location": {
    "lat": 53.7925714,
    "lng": -0.9836201000000001
  },
  "location_type": "APPROXIMATE",
  "viewport": {
    "northeast": {
      "lat": 53.8218872,
      "lng": -0.9341634999999999
    },
    "southwest": {
      "lat": 53.7598041,
      "lng": -1.0365264
    }
  }
},
"place_id": "ChIJY4aH6Q4jeUgR0B9t7Z75DAU",
"types": [
  "administrative_area_level_4",
  "political"
]
},
{
  "address_components": [{
    "long_name": "YO8",
    "short_name": "YO8",
    "types": [
      "postal_code",
      "postal_code_prefix"
    ]
  },
  {
    "long_name": "Selby",
    "short_name": "Selby",
    "types": [
      "postal_town"
    ]
  },
  {
    "long_name": "England",
    "short_name": "England",
    "types": [
      "administrative_area_level_1",
      "political"
    ]
  },
  {
    "long_name": "United Kingdom",
    "short_name": "GB",
    "types": [
      "country",
      "political"
    ]
  }
],
"formatted_address": "Selby YO8, UK",
"geometry": {
  "bounds": {
    "northeast": {
      "lat": 53.8620955,
      "lng": -0.8143208
    },
    "southwest": {
      "lat": 53.700854,
      "lng": -1.1939887
    }
  },
  "location": {
    "lat": 53.7978369,
    "lng": -1.0032842
  },
  "location_type": "APPROXIMATE",
  "viewport": {
    "northeast": {
      "lat": 53.8620955,
      "lng": -0.8143208
    },
    "southwest": {
      "lat": 53.700854,
      "lng": -1.1939887
    }
  }
},
"place_id": "ChIJj20O6gEWeUgR1phll-wRyls",
"types": [
  "postal_code",
  "postal_code_prefix"
]
},
{
  "address_components": [{
    "long_name": "Selby",
    "short_name": "Selby",
    "types": [
      "postal_town"
    ]
  },
  {
    "long_name": "England",
    "short_name": "England",
    "types": [
      "administrative_area_level_1",
      "political"
    ]
  },
  {
    "long_name": "United Kingdom",
    "short_name": "GB",
    "types": [
      "country",
      "political"
    ]
  }
],
"formatted_address": "Selby, UK",
"geometry": {
  "bounds": {
    "northeast": {
      "lat": 53.85938239999999,
      "lng": -0.8253381
    },
    "southwest": {
      "lat": 53.7009007,
      "lng": -1.1955835
    }
  },
  "location": {
    "lat": 53.7978369,
    "lng": -1.0032842
  },
  "location_type": "APPROXIMATE",
  "viewport": {
    "northeast": {
      "lat": 53.85938239999999,
      "lng": -0.8253381
    },
    "southwest": {
      "lat": 53.7009007,
      "lng": -1.1955835
    }
  }
},
"place_id": "ChIJW-phvyYjeUgRp4YN5SPtn3M",
"types": [
  "postal_town"
]
},
{
  "address_components": [{
    "long_name": "Selby",
    "short_name": "Selby",
    "types": [
      "administrative_area_level_3",
      "political"
    ]
  },
  {
    "long_name": "North Yorkshire",
    "short_name": "North Yorkshire",
    "types": [
      "administrative_area_level_2",
      "political"
    ]
  },
  {
    "long_name": "England",
    "short_name": "England",
    "types": [
      "administrative_area_level_1",
      "political"
    ]
  },
  {
    "long_name": "United Kingdom",
    "short_name": "GB",
    "types": [
      "country",
      "political"
    ]
  }
],
"formatted_address": "Selby, UK",
"geometry": {
  "bounds": {
    "northeast": {
      "lat": 53.935584,
      "lng": -0.9034008
    },
    "southwest": {
      "lat": 53.6210943,
      "lng": -1.3529652
    }
  },
  "location": {
    "lat": 53.7867568,
    "lng": -1.0677977
  },
  "location_type": "APPROXIMATE",
  "viewport": {
    "northeast": {
      "lat": 53.935584,
      "lng": -0.9034008
    },
    "southwest": {
      "lat": 53.6210943,
      "lng": -1.3529652
    }
  }
},
"place_id": "ChIJRzjbpCs8eUgRQONs7Z75DAQ",
"types": [
  "administrative_area_level_3",
  "political"
]
},
{
  "address_components": [{
    "long_name": "North Yorkshire",
    "short_name": "North Yorkshire",
    "types": [
      "administrative_area_level_2",
      "political"
    ]
  },
  {
    "long_name": "England",
    "short_name": "England",
    "types": [
      "administrative_area_level_1",
      "political"
    ]
  },
  {
    "long_name": "United Kingdom",
    "short_name": "GB",
    "types": [
      "country",
      "political"
    ]
  }
],
"formatted_address": "North Yorkshire, UK",
"geometry": {
  "bounds": {
    "northeast": {
      "lat": 54.56214379999999,
      "lng": -0.2123533
    },
    "southwest": {
      "lat": 53.6210943,
      "lng": -2.5647389
    }
  },
  "location": {
    "lat": 53.9915028,
    "lng": -1.5412015
  },
  "location_type": "APPROXIMATE",
  "viewport": {
    "northeast": {
      "lat": 54.56214379999999,
      "lng": -0.2123533
    },
    "southwest": {
      "lat": 53.6210943,
      "lng": -2.5647389
    }
  }
},
"place_id": "ChIJ8WWY4UDDeEgRt5-VyBki4MM",
"types": [
  "administrative_area_level_2",
  "political"
]
},
{
  "address_components": [{
    "long_name": "England",
    "short_name": "England",
    "types": [
      "administrative_area_level_1",
      "political"
    ]
  },
  {
    "long_name": "United Kingdom",
    "short_name": "GB",
    "types": [
      "country",
      "political"
    ]
  }
],
"formatted_address": "England, UK",
"geometry": {
  "bounds": {
    "northeast": {
      "lat": 55.81165979999999,
      "lng": 1.7629159
    },
    "southwest": {
      "lat": 49.8647411,
      "lng": -6.4185458
    }
  },
  "location": {
    "lat": 52.3555177,
    "lng": -1.1743197
  },
  "location_type": "APPROXIMATE",
  "viewport": {
    "northeast": {
      "lat": 55.81165979999999,
      "lng": 1.7629159
    },
    "southwest": {
      "lat": 49.8647411,
      "lng": -6.4185458
    }
  }
},
"place_id": "ChIJ39UebIqp0EcRqI4tMyWV4fQ",
"types": [
  "administrative_area_level_1",
  "political"
]
},
{
  "address_components": [{
    "long_name": "United Kingdom",
    "short_name": "GB",
    "types": [
      "country",
      "political"
    ]
    }],
    "formatted_address": "United Kingdom",
    "geometry": {
      "bounds": {
        "northeast": {
          "lat": 60.91569999999999,
          "lng": 33.9165549
        },
        "southwest": {
          "lat": 34.5614,
          "lng": -8.8988999
        }
      },
      "location": {
        "lat": 55.378051,
        "lng": -3.435973
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "northeast": {
          "lat": 60.91569999999999,
          "lng": 33.9165549
        },
        "southwest": {
          "lat": 34.5614,
          "lng": -8.8988999
        }
      }
    },
    "place_id": "ChIJqZHHQhE7WgIReiWIMkOg-MQ",
    "types": [
      "country",
      "political"
    ]
  }
],
"status": "OK"
}]

Hi @Felix_is_stuck,

I take it you already have the JSON payload you have shared in n8n?

If so, the reason you can’t access the individual items would be that this is currently a single item in n8n. So there’s only a single item to reference in your Function node.

So if you want to run through all address_components objects in all results objects and use any type you find as a key in a new n8n item with the corresponding long_name as the value, you could use a JavaScript snippet like this in your Function node:

let return_values = [];

for (result of items[0].json.results) {
  for (address_component of result.address_components) {
    for (ac_type of address_component.types) {
      console.log(ac_type);
      return_values.push({
        json: {
          [ac_type]: address_component.long_name
        }
      })
    }
  }
}

return return_values;

Though I am not really sure this is what you had in mind from looking at the result:

If you still struggle with this example, maybe you can share an example of what you’d like to see after your Function Node?

Thank you @MutedJam - This has helped immensely!!

Within my database I have 4 fields for each address:
-Address
-City
-State
-Zip
-Country

The database was initially built for US addresses but we can substitute State for Region and Zip for Postal Code. I somehow need to normalize the extracted data and map it to those fields.

Right now I’m left with two problems:

  1. The output from the function node spits each value out as a separate item, so I’m not sure how to reference any specific object, since whilst in our example above ‘postal_code’ is the 12th item, it could be different once Google’s API processes a different location.

  2. Some ‘Types’ have multiple values, the database should only accept 1, the most logical value for the field(s) it’s mapped to. Taken from the blog post I linked in my original question:

" so we can loop over the address components array one by one and detect it types so if it’s a “street_address” we can store it in our home and if it’s an “administrative_area_level_3”, “administrative_area_level_2” or an “administrative_area_level_1” it can be our region. but we only need to make an only for loop without too many if statements and we need also to make it choose which one is better so if there an “administrative_area_level_1” and level_2 we need to choose level_1 because it’s a higher level than level_2. hint: google also helps us with that: it gives us a sorted array so level_2 is before level_1."

To answer your question directly - if possible, I’d like to see the results reduced/mapped to 5 individually accessible keys/values output in a single JSON item.

Sorry, I am still not 100% sure how the desired output should look like. So all these items in the results array field belong to a single address? And you would like to fit this data in a single item with the fields you have listed (Address, City, State, Zip, Country)?

I am not familiar with the specific API you are using, but if the above assumptions are correct you should be able to use simple finds and if/else logic to get the data you’d like to see. In this new example I have replaced the Function node with Function Item to simplify the data structure a bit:

Example Workflow

You might need to tweak this a bit depending on what fields are the most relevant (and maybe account for edge cases I am not familiar with), but this should still be a good starting point I hope.

@MutedJam - you’ve really gone over and above with your detailed response/demo - thank you, it’s appreciated.

For additional context - I’m using Google Maps API to reverse geocode Lat/Long coordinates. I pass coordinates to Googles API, and it responds with a number of possible address matches - here I want to use the first one in the response, passed in ‘results[0]’ and append it to my database as the ‘most likely’ address match for the given coordinates.

I copied your example and amended the function so that it calls the data from Google’s response, so far so good…

The Function Items node then fails however (unchanged from your example):

“Cannot read property ‘find’ of undefined”

That would be because the data structure from your example is different from the one in your first post. My new example would be standalone so no longer requires the Item Lists node after you clarified what you wanted to see.

Thank you! Works a treat :slight_smile:

Is there a line I can add at the end to also passthrough the payload received on the input?
I tried ‘return items;’ but that didn’t work.

That’d be because the example code in the Function Item node would create a whole new item which is then returned. You could for example pass on the original item’s payload in the return item by adding a new property (for example "Payload": item):

(...)
let return_item = {
  "Address": result_address.trim(),
  "City": result_city,
  "State": result_state,
  "Zip": result_zip,
  "Country": result_country,
  "Payload": item
};

return return_item;

Alternatively, instead of creating a new item you could work with the original item and just add some new properties to it:

(...)
item["Address"] = result_address.trim();
item["City"] = result_city;
item["State"] = result_state;
item["Zip"] = result_zip;
item["Country"] = result_country;

return item;
1 Like

A little confused now!
A attempted to reuse @MutedJam’s example code on the output of another Google API where the output looks almost identical - ‘address_components’ are delivered inside ‘result’ rather than ‘results’. I can only find 1 reference to item.results and I tried to update that - but I just get ‘item.result.find’ is not a function.

Here is the output from the API:

[

{

"html_attributions": [

],

"result": {

"address_components": [

{

"long_name": "106",

"short_name": "106",

"types": [

"street_number"

]

},

{

"long_name": "Landsberger Allee",

"short_name": "Landsberger Allee",

"types": [

"route"

]

},

{

"long_name": "Bezirk Lichtenberg",

"short_name": "Bezirk Lichtenberg",

"types": [

"sublocality_level_1",

"sublocality",

"political"

]

},

{

"long_name": "Berlin",

"short_name": "Berlin",

"types": [

"locality",

"political"

]

},

{

"long_name": "Kreisfreie Stadt Berlin",

"short_name": "Kreisfreie Stadt Berlin",

"types": [

"administrative_area_level_3",

"political"

]

},

{

"long_name": "Berlin",

"short_name": "BE",

"types": [

"administrative_area_level_1",

"political"

]

},

{

"long_name": "Germany",

"short_name": "DE",

"types": [

"country",

"political"

]

},

{

"long_name": "10369",

"short_name": "10369",

"types": [

"postal_code"

]

}

],

"adr_address": "<span class="street-address">Landsberger Allee 106</span>, <span class="postal-code">10369</span> <span class="locality">Berlin</span>, <span class="country-name">Germany</span>",

"business_status": "OPERATIONAL",

"formatted_address": "Landsberger Allee 106, 10369 Berlin, Germany",

"formatted_phone_number": "030 4530530",

"geometry": {

"location": {

"lat": 52.52845490000001,

"lng": 13.4571751

},

"viewport": {

"northeast": {

"lat": 52.5298953802915,

"lng": 13.4585103302915

},

"southwest": {

"lat": 52.5271974197085,

"lng": 13.4558123697085

}

}

},

"icon": "https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/lodging-71.png",

"icon_background_color": "#909CE1",

"icon_mask_base_uri": "https://maps.gstatic.com/mapfiles/place_api/icons/v2/hotel_pinlet",

"international_phone_number": "+49 30 4530530",

"name": "Vienna House Andel’s Berlin",

"photos": [

{

"height": 1999,

"html_attributions": [

"<a href="https://maps.google.com/maps/contrib/113993569433932320225">Vienna House Andel’s Berlin</a>"

],

"photo_reference": "Aap_uEDylH1Z6nAXyYRcPasb5WGDy5miMOc0pcvf2VtSi0qimONXXLsVRlxrMUocZ-Z9SDc9spPrPkNjVEioEqoUp_fVax2kPDHlv8ofzIDkfuZd-rkdZZy9zeXU7kUDN2qRBeZu1PD3FNvfleyGErOUfcPL7oLFYwjuACGW3Y2wIXcnF0vF",

"width": 3000

},

{

"height": 2002,

"html_attributions": [

"<a href="https://maps.google.com/maps/contrib/113993569433932320225">Vienna House Andel’s Berlin</a>"

],

"photo_reference": "Aap_uEA1ZtO-wB5rUqtpxfMFw5Qdct3Ogr01nbwSKNTm2mWVBbK_LtI_Hd5QlrT96T-LLMrnp6wmIeZ8-NMED1tQRzOA6zI-iAh3UgdI58al_ljMvDsEmPWi_WMywgTD0dJE4QajZgQ_xYSyuOb3lbCE1KCbaRqkBOzhUHWPBDEncxBTU62F",

"width": 3000

},

{

"height": 2000,

"html_attributions": [

"<a href="https://maps.google.com/maps/contrib/113993569433932320225">Vienna House Andel’s Berlin</a>"

],

"photo_reference": "Aap_uEAskGLKtt9eCZA0cBGCpCSuoakDg4u4IsET6hXsb_ucgVDZHJWjoix7u0_m1yaE8y926ffahPArLbOCfFli44oGTn_VHux3K950Wq3EZ9-UfF4gLruWnXduRiwFnkal1wKuCwqkdV4xPyHOaH-dB-cujul9ey8TYsK2Ai4NZ7qKLYXK",

"width": 2997

},

{

"height": 2002,

"html_attributions": [

"<a href="https://maps.google.com/maps/contrib/113993569433932320225">Vienna House Andel’s Berlin</a>"

],

"photo_reference": "Aap_uEDWFWecpCYFTnzF4KTlNx1RDk6c0YZZxh2OGYdQyA9f0LBvvyOFkoZs9QmoYBvVem4Exm-tCwAob1tcDPfm8gK6tnuDL95O3YJ5Jl7vXvN3WdFR2FLPTWuq04hOvI9SXXz_LNud4xTI3MiemdvJL_HpoUkTGa_ra-XsBxGOGLoxO76y",

"width": 3000

},

{

"height": 2000,

"html_attributions": [

"<a href="https://maps.google.com/maps/contrib/113993569433932320225">Vienna House Andel’s Berlin</a>"

],

"photo_reference": "Aap_uEBA7AuWmScGZEFFHf__JJdpIn6NzaXAhqGQmXm9G98UromhJp1Dqa_TDBbrOobnvK_YvBQGBbFTPrjIF2JLQjYTUfRg2p1tILrSAR5sm1wy0QqbaprnGGugSUbtfKl3eP-TiGMiCal-pWe8lpGiGes1uisMcXwCcsrIh6FRPcrWTXXN",

"width": 2997

},

{

"height": 2000,

"html_attributions": [

"<a href="https://maps.google.com/maps/contrib/113993569433932320225">Vienna House Andel’s Berlin</a>"

],

"photo_reference": "Aap_uEBSU-_cDIxdXncrcq5HvXblR9mIKYMkUibOfdq1hnIGvmPZ-ZFGqwhoz0CBZjYVXl2oQakZEevNIWSjJw5vuR6axiXmmRgynzd8Vfgy5XVOkdZSI4xhqpZkAUZDmUfsgTXBxt2l7ij6__iQ2iE07DzuKHZNVvx0x7M2vHR2QG4MPYx4",

"width": 2997

},

{

"height": 2000,

"html_attributions": [

"<a href="https://maps.google.com/maps/contrib/113993569433932320225">Vienna House Andel’s Berlin</a>"

],

"photo_reference": "Aap_uEBYsmz5apJ2gHaRRg_is5ifV9z62cRhiwZtatOcC7hgZYy35yRL8bw1Omt5vP3kewkV-dXV3icPD9RtDebWtEK2YMVEZhQoZw1JQCyWuYs4kZ7qn9d19CcNUgnTWbhgGu2zmBGe0pdKk3-KuRUk-CGYaCiJjSPEUt4BTzXFY6dADSR1",

"width": 2997

},

{

"height": 2000,

"html_attributions": [

"<a href="https://maps.google.com/maps/contrib/113993569433932320225">Vienna House Andel’s Berlin</a>"

],

"photo_reference": "Aap_uEDDGfSCx34eynZAWeGneZCGpli7Vq7m1CNG2I-CkHPwnmlGCNLgS_OGY1DFIaAmewNVYMVoGosQI-bVEG_pwnZ7YbFGuDXDUmADBqpMz4hDiBT_ZzYlOo5KEhXQ0HaUSAvY0cs_WIXe3ORi5IWSXCtcdkKHLAPEOxFZdv-80fopldMR",

"width": 3000

},

{

"height": 2000,

"html_attributions": [

"<a href="https://maps.google.com/maps/contrib/113993569433932320225">Vienna House Andel’s Berlin</a>"

],

"photo_reference": "Aap_uEAiOU0951oVWy2M_VI_24T-0cXC0bKucNa12BC_q_eaL4XPKwIAgPRG4azxHXLy3n0x-NE8lGXUPJ21uroRTb_bBf2SPI_RcZxTJIyBCsufabISQLEFZH61JCHn95IW7NYSgwSrdmTBHEQdHsuFTc4K2Nv1bni2HrmxTZlnt2JXlodg",

"width": 2997

},

{

"height": 2000,

"html_attributions": [

"<a href="https://maps.google.com/maps/contrib/113993569433932320225">Vienna House Andel’s Berlin</a>"

],

"photo_reference": "Aap_uEB9fNNE-i752UVg4b3fR7czMqV4qFDLnbT7YYXWGN843HRuOtzLZYQmveoxM2HIovXZ8NDNIcRf2EWEre9KybW5ffh3N7_9bSodgSienLjvHHc8HfW9wU9M_h6UaiKs_WAqKnY_6ElodSdYfGwIUh8r3_rGCFXPA4KYE_WGlL5WWBud",

"width": 3000

}

],

"place_id": "ChIJA65CgXpOqEcRYAJJ5gU_CF0",

"plus_code": {

"compound_code": "GFH4+9V Berlin, Germany",

"global_code": "9F4MGFH4+9V"

},

"rating": 4.4,

"reference": "ChIJA65CgXpOqEcRYAJJ5gU_CF0",

"reviews": [

{

"author_name": "Florin Marian Badea",

"author_url": "https://www.google.com/maps/contrib/113090820953727740154/reviews",

"language": "en",

"profile_photo_url": "https://lh3.googleusercontent.com/a/AATXAJzc95XSccyYvk1dX2Qx-k7uqu1-SOVfDCGOxo_m=s128-c0x00000000-cc-rp-mo-ba3",

"rating": 5,

"relative_time_description": "a month ago",

"text": "Large and clean rooms, great breakfast. Friendly staff. Close to train station. Underground garage with plenty parking lots. Spent two nights here and we felt very good . A cocktail at the 14 floor bar is a must! 😄 The view is amazing!",

"time": 1636789790

},

{

"author_name": "Hanna Juhler",

"author_url": "https://www.google.com/maps/contrib/109543380684288259773/reviews",

"language": "en",

"profile_photo_url": "https://lh3.googleusercontent.com/a-/AOh14GgGMPDMyHYTfLQ9Enm9QVK2hF386kq78_idS6Ed=s128-c0x00000000-cc-rp-mo",

"rating": 5,

"relative_time_description": "4 months ago",

"text": "Ths is my go-to hotel when I stay in Berlin. Rooms are large with all necessary comforts and the hotel is in a convenient location for all the sights. The breakfast buffet has a great selection and all staff are friendly and helpful.",

"time": 1630323768

},

{

"author_name": "Kirsi Rinta",

"author_url": "https://www.google.com/maps/contrib/102338740470303826609/reviews",

"language": "en",

"profile_photo_url": "https://lh3.googleusercontent.com/a/AATXAJzlhbyP-BNKMxnloMT3411FnH_tg1jTBBiXwnDn=s128-c0x00000000-cc-rp-mo-ba3",

"rating": 5,

"relative_time_description": "2 weeks ago",

"text": "Very very fine hotel, really clean and so great Sky bar in 14 floor. Room and the breakfast perfect, recommended 👍",

"time": 1640112428

},

{

"author_name": "Manos Nikolidakis",

"author_url": "https://www.google.com/maps/contrib/117805765696331415268/reviews",

"language": "en",

"profile_photo_url": "https://lh3.googleusercontent.com/a/AATXAJycX60ax41vSSGEkZLl1hms7EJ96z2fH-Y51BI7=s128-c0x00000000-cc-rp-mo",

"rating": 1,

"relative_time_description": "4 months ago",

"text": "I went to the restaurant on the top floor and after a hour and a half they told me that they lost my order and there is nothing else to eat and they let me go...11 in the night! If I was the owner of this restaurant I would call right now the manager and fire him..",

"time": 1630014635

},

{

"author_name": "Isis",

"author_url": "https://www.google.com/maps/contrib/115195030738488152502/reviews",

"language": "en",

"profile_photo_url": "https://lh3.googleusercontent.com/a-/AOh14GizPrZsPE9rWKcqLj6xiuaehbKiEetcv6ZudGqpRA=s128-c0x00000000-cc-rp-mo-ba3",

"rating": 4,

"relative_time_description": "4 months ago",

"text": "Nice hotel! Only the view ( and then the noise ) on the railway wasn't expected for the price of the room. Nice breakfast with lot of choices.",

"time": 1630361853

}

],

"types": [

"spa",

"lodging",

"bar",

"restaurant",

"food",

"point_of_interest",

"establishment"

],

"url": "https://maps.google.com/?cid=6703677339911914080",

"user_ratings_total": 5258,

"utc_offset": 60,

"vicinity": "Landsberger Allee 106, Berlin",

"website": "http://www.viennahouse.com/andels-berlin/"

},

"status": "OK"

}

]

What am I missing here?

Hi @Felix_is_stuck, in your recent example, result isn’t an array which is why .find() will not work on it. If the array you are searching data in is item.result.address_components, you would need to append the .find() method to this. To find the street number and write in a separate field you could do for example this in your Function Item node:

item.street_number = item.result.address_components.find(e => e.types.includes("street_number")).long_name;
return item;

Seeing how this problem gets more and more complex, I think it could make a good feature request for a new node that simplifies working with this particular API, maybe you want to raise it?

@MutedJam - Thank you for the hint you shared, and yes I agree, this would be a great feature - I’d be happy to contribute my request!

Using the code you shared above, I was able to cobble together a FunctionItem node that simply outputs the address_components variables if they exist, and then using the ternary operator in a Set node, able to manipulate the data to get the address formatted as I would like it.

I’m sure for those more adept with JS this would be very easy (I don’t fall into that category!).

In case this is useful to anyone else here, this is what I ended up with:

if (typeof item.result.address_components.find(e => e.types.includes("street_number")) !== 'undefined') {
    item["street_number"] = item.result.address_components.find(e => e.types.includes("street_number")).long_name;
}

if (typeof item.result.address_components.find(e => e.types.includes("route")) !== 'undefined') {
    item["route"] = item.result.address_components.find(e => e.types.includes("route")).long_name;
}

if (typeof item.result.address_components.find(e => e.types.includes("locality")) !== 'undefined') {
    item["locality"] = item.result.address_components.find(e => e.types.includes("locality")).long_name;
}

if (typeof item.result.address_components.find(e => e.types.includes("postal_town")) !== 'undefined') {
    item["postal_town"] = item.result.address_components.find(e => e.types.includes("postal_town")).long_name;
}

if (typeof item.result.address_components.find(e => e.types.includes("administrative_area_level_1")) !== 'undefined') {
    item["administrative_area_level_1"] = item.result.address_components.find(e => e.types.includes("administrative_area_level_1")).long_name;
}

if (typeof item.result.address_components.find(e => e.types.includes("administrative_area_level_2")) !== 'undefined') {
    item["administrative_area_level_2"] = item.result.address_components.find(e => e.types.includes("administrative_area_level_2")).long_name;
}

if (typeof item.result.address_components.find(e => e.types.includes("administrative_area_level_3")) !== 'undefined') {
    item["administrative_area_level_3"] = item.result.address_components.find(e => e.types.includes("administrative_area_level_3")).long_name;
}

if (typeof item.result.address_components.find(e => e.types.includes("political")) !== 'undefined') {
    item["political"] = item.result.address_components.find(e => e.types.includes("political")).long_name;
}

if (typeof item.result.address_components.find(e => e.types.includes("postal_code")) !== 'undefined') {
    item["postal_code"] = item.result.address_components.find(e => e.types.includes("postal_code")).long_name;
}

if (typeof item.result.address_components.find(e => e.types.includes("country")) !== 'undefined') {
    item["country"] = item.result.address_components.find(e => e.types.includes("country")).long_name;
}

return item;
1 Like