Hey everyone! I’m working on a project where I need to combine two JSON arrays from the Zammad API into one structure using n8n. I’ve got the ticket info and the related articles, but I’m not sure how to merge them correctly.
I need to create a new object with ‘Ticket’ and ‘articles’ as the main keys. The ticket info should go under ‘Ticket’, and the articles should be in an array under ‘articles’.
Here’s a basic example of what I’m trying to achieve:
const result = {
Ticket: ticketData[0],
articles: articleData
};
But I’m not really a JavaScript pro, so I’m struggling to get it right. Any tips on how to do this in n8n’s JavaScript node? I’ve tried looking up JSON merging techniques, but the nested structure is throwing me off.
Thanks in advance for any help! I’m excited to learn how to do this properly.
I’ve worked with JSON in n8n before, and here’s a solution that might help you out:
const result = {
Ticket: ticketData[0],
articles: articleData
};
return { json: result };
This code creates a new object with ‘Ticket’ containing the first element of your ticketData array and ‘articles’ containing all elements from your articleData array.
If you need to associate articles with specific tickets, you could modify it like this:
const result = ticketData.map(ticket => ({
Ticket: ticket,
articles: articleData.filter(article => article.ticketId === ticket.id)
}));
return { json: result };
This assumes articles have a ticketId field. It creates an array of objects, each representing a ticket with its associated articles.
Remember to test your code thoroughly in n8n’s JavaScript node to ensure it works with your specific data structure.
Hey Ethan, I’ve tackled similar JSON merging challenges in n8n before. Here’s a slightly different approach that might work for you:
const mergedData = ticketData.reduce((acc, ticket) => {
acc.push({
Ticket: ticket,
articles: articleData.filter(article => article.ticket_id === ticket.id)
});
return acc;
}, []);
return { json: mergedData };
This code uses reduce() to create an array of objects, each containing a ticket and its associated articles. It assumes your articles have a ticket_id field to match them with the correct ticket.
If you’re dealing with a large dataset, you might want to consider using a Map for better performance:
const articleMap = new Map(articleData.map(a => [a.ticket_id, a]));
const mergedData = ticketData.map(ticket => ({
Ticket: ticket,
articles: articleMap.get(ticket.id) || []
}));
This approach first creates a Map of articles keyed by ticket_id, then maps over the tickets to create the final structure. It’s more efficient for larger datasets.
Remember to adjust the field names (like ticket_id) to match your actual data structure. Hope this helps!
hey Ethan, i’ve messed around with json in n8n before. here’s a quick snippet that might help:
const result = {
Ticket: ticketData[0],
articles: articleData.map(a => ({ ...a, ticketId: ticketData[0].id }))
};
return { json: result };
this assumes you want the first ticket and all articles. it also adds the ticket id to each article. hope this helps!