Importing CSV containing addresses to CMS

I want to upload a CSV containing a set of addresses to the CMS to use with the Google Maps widget. I have the lat and lon for each address and I also have the full address string for each location.

When I upload my CSV to the CMS I get a warning saying that the “Addresses“ (which I upload in a CMS column “Address” are not valid address. Then I click on each warning sign and I presume that Wix geocodes the address strings i imported to get address objects that can be connected to the Google Map widget.

Problem is: I need to import hundreds of records in my CMS so I don’t want to have to click on each error to trigger the geocoding… How can I fix this?

I imagine the import is looking for the full address object, likely in this format:

{
  formatted: "500 Terry A Francois Blvd, San Francisco, CA 94158, USA",
  location: {
    latitude: 37.7703718,
    longitude: -122.38712479999998,
  },
  streetAddress: {
    name: "Terry A Francois Blvd",
    number: "500",
  },
  city: "SF",
  subdivision: "CA",
  country: "US",
  postalCode: "94158",
}

My recommendation would be something along the lines of:

  • Import the lat/long and Address string as individual fields
  • Use a data hook, so that when the item is inserted into the collection it constructs the Address into an object and sets the address field value - About Data Hooks | SDK

This way, you can provide the data in the format you have, and then during the import process, the hooks will run and construct it in the expected format.

Thanks @noahlovell - but how can I “cast“ a string into an address object? Is there a constructor I need to use? Or a Google Maps geocoding util function available through the SDK?

Support just told me this: “The thing here is, the address field in the CMS collection is a field that works internally with proprietary Wix information“

So basically I was advised to manually click on ALL the CMS import errors to cast my addresses into “wix proprietary address“ objects… I have tens of thousands of entries in the collection….

Just built our a working solution :slight_smile:


First up, we’ll need to setup our CMS Collection in Wix. At a minimum, we’ll need these fields:

  • title (should be Text type)
  • latitude (should be Number type)
  • longitude (should be Number type)
  • address (should be an Address type)

It’ll look something like:


Now, we need to setup our data hooks. I recommend following the steps in the article I shared before on how to do start this.

For this, we’ll be specifically using the beforeInsert which will run before each item is inserted into the collection.

Since a valid address object only needs to contain the location (with lat and long values), and a formatted name at a minimum, and we have that info, we can do the following.

I’d recommend taking a backup of any data before doing this incase you need it :slight_smile:

export function Locations_beforeInsert(item, context) {
	// Only construct the address if both latitude and longitude values are present in the item
    if (item.latitude && item.longitude) {
		// Construct the minimum viable address object
        item.address = {
            "location": {
                "latitude": item.latitude, // Use the latitude value from the item
                "longitude": item.longitude // Use the longitude value from the item
            },
            "formatted": item.title
        }
    }

	// Return the new item
    return item
}

Note: you might need to check your field keys match the ones I’ve used in the above code


Now, we need our CSV of data. (I’ll apologise for the names and lat/longs not being accurate, Gemini gave me some demo data for this :sweat_smile:)


Go ahead and import the data, mapping the columns of your CSV to the fields in the collection


Once imported, you now have correctly formatted address field that was constructed from the Title, Latitude and Longitude values, and can connect to Google Maps


Hope this helps!