Hey everyone! I’m working on an n8n workflow and I’m stuck trying to combine two JSON arrays. I’ve got ticket data from the Zammad API and I need to merge it with the corresponding ticket articles. Here’s what I’m trying to do: 1. Take the ticket object and wrap it in a ‘Ticket’ key. 2. Take the array of ticket articles and put it under an ‘articles’ key. 3. Combine these into a single JSON structure. I’ve been looking into JSON merging and concatenation, but I’m having trouble with the naming part (adding ‘Ticket:’ and ‘articles:’). I’m not very experienced with JavaScript, so I could really use some help! Here’s a simplified example of what I’m aiming for:
const result = {
Ticket: {
id: 123,
title: 'Example Ticket'
},
articles: [
{
id: 456,
body: 'First article'
},
{
id: 789,
body: 'Second article'
}
]
}
Can anyone help me write the JavaScript code to achieve this in n8n? Thanks in advance for any assistance!
I’ve dealt with similar JSON merging challenges in n8n. Here’s a slightly different approach that might be useful:
const mergeTicketData = (ticket, articles) => ({
Ticket: { ...ticket },
articles: [...articles]
});
// Usage in n8n
const combinedData = mergeTicketData(items[0].json.ticket, items[0].json.articles);
This method uses the spread operator to create a new object and array, which can help avoid unexpected mutations. It’s also a bit more concise.
In n8n, you’d typically use this in a Function node. Make sure your input data is properly structured. If you’re fetching from the Zammad API, you might need to do some data transformation first.
Also, consider error handling. You could add checks to ensure ‘ticket’ and ‘articles’ exist and are in the correct format before merging. This can save you headaches down the line if the API response changes.
hey neo_stars! i’ve dealt with similar issues in n8n. here’s a quick solution:
const result = {
Ticket: ticket, // assuming 'ticket' is your ticket object
articles: articles // assuming 'articles' is your array of articles
};
this should give you the structure you want. hope it helps! lemme know if you need more info
I’ve encountered this situation before in n8n workflows. Here’s a more detailed approach that should work for you:
const combineTicketData = (ticket, articles) => {
return {
Ticket: ticket,
articles: articles
};
};
// Assuming you have your ticket and articles data
const result = combineTicketData(ticketData, articleArray);
This function takes your ticket object and articles array as parameters, then constructs the exact structure you’re looking for. You can call this function within your n8n workflow wherever you need to merge the data.
Remember to replace ‘ticketData’ and ‘articleArray’ with your actual data sources in n8n. This method is flexible and can handle various ticket and article structures.