Search code examples
javascriptsendgridzapier

Zapier Javascript: Unexpected token ]


In a Zapier Zap, I'm extracting data from Google Sheets and using JS to prettify it to later send in an email. I'm bumping into an error with the following message:

SyntaxError: Unexpected token ]

My code in zap

const data = {
    "list_ids": [
    "a0b30126-69d6-4822-ac06-bf76c3ff4770"
    ],
    "contacts": [
    {
        "email": "email",
        "first_name": "name",
        "custom_fields": {
        "e5_T": "list",
        "e6_T": "y",
        "e7_T": "z"
    }
    ]
}

const res = await fetch('https://api.sendgrid.com/v3/marketing/contacts', {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json',
        'authorization': 'Bearer <<my api key>>'
    },
    body: JSON.stringify(data),
    })

.then(response => response.json())
.then(data => {
    console.log('Success:', data);
    })
.catch((error) => {
    console.error('Error:', error);
    });

return { res };

I used code from this blog https://joelaguero.com/how-to-add-a-sendgrid-contact-with-zapier-code-snippets/. Author said it worked for him.


Solution

  • It's because you have more { than }. You probably forgot about closing } in custom_fields or in contacts.