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.
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.