Merging two JSON arrays into a single structure using n8n and JavaScript

Hey everyone, I’m working on a project where I need to combine data from the Zammad API. I’ve got two JSON objects: one for a ticket and another for the related articles. My goal is to merge these into a single structure.

Here’s what I’m trying to achieve:

const combinedData = {
  Ticket: ticketData[0],
  articles: articleData
};

I’m using n8n in my workflow and can add custom JavaScript to do this merging. The challenge for me is properly labeling the ticket and articles parts of the combined data. I’m quite new to JavaScript and would appreciate any guidance or code snippets that can help me out.

Thanks in advance for your help!

I’ve dealt with similar data merging challenges in my projects. One approach that’s worked well for me is using the Object.assign() method in JavaScript. Here’s a snippet you could try in n8n’s Function node:

const mergedData = Object.assign({}, 
  { Ticket: items[0].json.ticket[0] },
  { articles: items[1].json.article_list }
);

return [{ json: mergedData }];

This creates a new object and copies the properties from your ticket and article data into it. Make sure your data is structured as expected in the items array. You might need to adjust the indices or property names based on your specific n8n workflow setup.

Also, consider error handling. You could add checks to ensure the data exists before merging:

if (!items[0].json.ticket || !items[1].json.article_list) {
  throw new Error('Missing required data');
}

This has helped me catch issues early in my workflows. Good luck with your Zammad integration!

hey there, i’ve done similar stuff before. here’s a quick way to do it in n8n:

let combined = {
  Ticket: $input.all()[0].json.ticket[0],
  articles: $input.all()[1].json.article_list
};

return [{json: combined}];

this should work if ur ticket n article data are in separate nodes. just make sure the indexes match ur workflow. good luck with ur project!

I’ve encountered a similar situation in my work with n8n and API integrations. Here’s a straightforward approach you can use in the Function node:

const ticketData = items[0].json.ticket;
const articleData = items[1].json.article_list;

const combinedData = {
  Ticket: ticketData,
  articles: articleData
};

return [{ json: combinedData }];

This code assumes your ticket data is in the first item and article data in the second. Adjust the indices if needed. The structure matches your desired output, with ‘Ticket’ capitalized as you specified. Remember to set the ‘Keep Only Set’ option in the Function node to ensure clean output. Hope this helps with your Zammad integration!