I’m working on a project where I need to combine data from the Zammad API. I have two JSON objects: one for a ticket and another for the ticket’s articles. I want to merge them into a single structure.
Here’s what I’m trying to achieve:
const combinedData = {
Ticket: ticketData[0],
articles: articleData
};
The ticket data is in an array with one object, while the articles data is an array of objects.
I’m using n8n and can add JavaScript code to my workflow. However, I’m not very experienced with JavaScript. I’ve looked into JSON merging and concatenation, but I’m struggling with adding the “Ticket” and “articles” labels.
Can anyone help me write the JavaScript code to combine these JSON objects in n8n? I’d really appreciate any guidance or examples. Thanks!
hey hazel, i’ve done somethin similar before. here’s a quick way to do it in n8n:
const ticketData = $input.all()[0].json.ticketData;
const articleData = $input.all()[0].json.articleData;
const combinedData = {
Ticket: ticketData[0],
articles: articleData
};
return { json: combinedData };
just pop this in a Function node and you should be good to go. Make sure to check your input data structure matches what’s assumed here. good luck!
Having worked extensively with n8n and JSON manipulation, I can offer some insights. In your case, a Function node is indeed the way to go. You’ll want to access the input data, then construct your combined object. Here’s a straightforward approach:
const ticketData = $input.all()[0].json.ticketData;
const articleData = $input.all()[0].json.articleData;
const combinedData = {
Ticket: ticketData[0],
articles: articleData
};
return { json: combinedData };
This code assumes your input data is structured as you described. It’s crucial to verify the structure of your incoming data and adjust the code accordingly. Also, remember to set the ‘Add Output’ option in the Function node settings to ensure the result is passed to the next node in your workflow.
I encountered a similar scenario when trying to merge two JSON arrays in n8n. In my project, I was combining ticket data and its related articles into one structure. I used a Function node to extract the arrays from the incoming items, then constructed a new JSON object with clearly labeled properties. For instance:
const ticketData = items[0].json.ticket;
const articleData = items[0].json.articles;
const combinedData = {
Ticket: ticketData[0],
articles: articleData
};
return { json: combinedData };
I also found that testing the output with a Debug node helped confirm the structure was as expected. This method ensured that the data was properly merged for further processing.