Using JavaScript in Zapier Code to format incoming data as required JSON

I’m trying to handle an API that returns its data in a format that Zapier doesn’t accept. The API gives multiple JSON objects that need to be included in an array.

API Output:

{"city":"Warren Township","country":"United States"}
{"city":"New York","country":"United States"}
{"city":"Stamford","country":"United States"}

Zapier’s Requirement:

[{"city":"Warren Township","country":"United States"},
{"city":"New York","country":"United States"},
{"city":"Stamford","country":"United States"}]

I attempted to fix this issue with a Code by Zapier step using the following JavaScript:

let url = 'https://api.iterable.com/api/export/data.json?dataTypeName=emailOpen&range=Today&onlyFields=email&onlyFields=createdAt&onlyFields=templateId&onlyFields=messageId&onlyFields=campaigniD&onlyFields=contentID'
let iterableData = 'undefined'

let response = await fetch(url, {
    method: "GET",
    headers: {
    'Api-Key': '----------redacted----------------',
    },
});

iterableData = await response.text()

iterableData = iterableData.replace(/\}/g, ",")
iterableData = "[" + iterableData.slice(0, -2) + "]"
let iterableJSON = JSON.parse(iterableData)

output = {iterableJSON}

This method effectively modifies the API response into a correctly formatted JSON array for Zapier. The main steps involve replacing the endings of objects and enclosing the whole response in brackets, followed by parsing it.

Your approach works, but string replacement can break on edge cases. Better to split the response by newlines and parse each JSON object separately:

let response = await fetch(url, {
    method: "GET",
    headers: {
        'Api-Key': 'your-key-here'
    }
});

let data = await response.text();
let lines = data.trim().split('\n');
let jsonArray = lines.map(line => JSON.parse(line));

output = {data: jsonArray};

This treats each line as a separate JSON object (JSONL format) instead of doing string manipulation. You won’t get parsing errors if your data has commas or brackets in string values.

you’re working with jsonl format - that’s standard for streaming apis. skip the regex and try this instead: let jsonObjs = response.text().split('\n').filter(l => l.trim()).map(JSON.parse); output = {results: jsonObjs}; much cleaner than string manipulation and it handles empty lines for ya.

I’ve hit this JSONL parsing issue before. Your string replacement method will break if any JSON values contain closing braces. Here’s what works better: javascript let response = await fetch(url, { method: "GET", headers: { 'Api-Key': 'your-api-key' } }); let rawData = await response.text(); let validObjects = []; rawData.split('\n').forEach(line => { if (line.trim()) { try { validObjects.push(JSON.parse(line)); } catch (e) { console.log('Skipped invalid JSON:', line); } } }); output = {data: validObjects}; This handles blank lines and malformed responses without killing your entire Zap. The try-catch saves you from losing everything over one bad line.