Combining two JSON arrays into a unified structure using JavaScript in n8n

Working with Helpdesk API Data in n8n

I am building an n8n workflow that needs to retrieve information from a helpdesk API. This API provides two distinct JSON arrays that I want to merge into one coherent object.

First API response includes support requests:

[
  {
    "request_id": 42,
    "department_id": 1,
    "urgency_level": 3,
    "status_id": 1,
    "company_id": null,
    "reference_number": "REQ-2023-001",
    "subject": "Login Issue - Database Access",
    "assigned_to": 7,
    "requester_id": 15,
    "description": null,
    "initial_response_time": "2023-03-20T09:15:22.456Z",
    "escalation_deadline": null,
    "response_time_minutes": 0,
    "response_delay_minutes": 2880,
    "resolution_time": null,
    "resolution_deadline": null,
    "total_resolution_minutes": null,
    "resolution_delay_minutes": null,
    "next_escalation_time": null,
    "estimated_work_minutes": null,
    "actual_work_minutes": null,
    "last_activity_time": "2023-03-20T09:22:15.789Z",
    "last_agent_contact": "2023-03-20T09:18:33.123Z",
    "last_customer_contact": "2023-03-20T09:22:15.789Z",
    "last_assignment_change": "2023-03-20T14:30:12.654Z",
    "initial_contact_method": 2,
    "initial_sender_type": 1,
    "total_messages": 5,
    "next_escalation": null,
    "hold_until": null,
    "category": null,
    "billable_hours": null,
    "settings": {
      "contact_method_id": 2,
      "sla_calculation": {
        "initial_response_time": "2023-03-20T09:15:22.456Z",
        "last_modification_time": "2023-03-20T09:22:15.789Z",
        "last_interaction_time": "2023-03-20T09:22:15.789Z",
        "service_level_id": 1,
        "sla_last_updated": "2023-04-10T11:45:33.987Z",
        "schedule_id": 1,
        "schedule_last_updated": "2023-04-12T08:30:45.321Z",
        "escalation_active": true
      }
    },
    "last_modified_by": 7,
    "created_by_user": 15,
    "creation_timestamp": "2023-03-20T09:15:22.401Z",
    "modification_timestamp": "2023-04-18T12:15:44.876Z",
    "final_resolution_time": null
  }
]

Second API response contains conversation history:

[
  {
    "message_id": 245,
    "request_id": 42,
    "message_type": 2,
    "sender_type": 2,
    "sender_email": "john.doe <[email protected]>",
    "recipient_email": "[email protected]",
    "cc_recipients": null,
    "message_subject": "Login Issue - Database Access",
    "reply_to_address": null,
    "unique_message_id": "<[email protected]>",
    "message_hash": "1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p",
    "in_response_to": null,
    "message_format": "text/plain",
    "email_references": null,
    "message_content": "\nI cannot access the database system after the recent update.\n\n-- \nBest regards | John Doe | Ext: 1234 | Mobile: +1-555-0123\n",
    "private_note": false,
    "message_settings": {
      "auto_reply_sent": true,
      "is_automated_response": false
    },
    "last_modified_by": 7,
    "created_by_user": 15,
    "original_author_id": null,
    "creation_timestamp": "2023-03-20T09:15:22.456Z",
    "modification_timestamp": "2023-03-20T09:15:22.456Z",
    "file_attachments": [],
    "communication_type": "email",
    "sender_role": "Customer",
    "created_by_email": "[email protected]",
    "modified_by_email": "[email protected]"
  }
]

Desired output structure:

{
  "SupportRequest": {
    "request_id": 42,
    "department_id": 1,
    // ... remaining request data
  },
  "messages": [
    {
      "message_id": 245,
      "request_id": 42,
      // ... remaining message data
    }
  ]
}

I have a custom Node.js service that requires this combined format. I seek guidance on how to write JavaScript in n8n to merge these two JSON responses into a single object with labeled sections.

I’ve looked into methods for merging JSON, but I find it challenging to include property names such as “SupportRequest” and “messages”. I’m new to JavaScript, so I would really appreciate any guidance!

Thank you for your help!

try this approach - I use Object.assign() for this stuff in n8n workflows:

const requests = $json.supportRequests || []; // your first api data
const conversations = $json.messages || []; // second api response

const merged = Object.assign({}, {
  SupportRequest: requests[0],
  messages: conversations
});

return merged;

You’re just creating a new object with named properties pointing to your existing data. Way simpler than trying to merge arrays - you’re not combining them, just wrapping them in a structure.

I’ve dealt with similar API merging scenarios in n8n before. You need to destructure your arrays properly and rebuild them into your desired object structure. Here’s what works for me:

// Assuming your first array is in items[0] and second in items[1]
const supportRequestData = $input.first().json; // First API response
const messagesData = $input.last().json; // Second API response

// Create the combined structure
const combinedResult = {
  SupportRequest: supportRequestData[0], // Take first element from support requests
  messages: messagesData // Keep messages as array
};

return combinedResult;

Make sure you handle cases where the arrays might be empty or undefined. I usually add basic validation like checking supportRequestData.length > 0 before accessing the first element. Also, if you have multiple support requests, you might want to filter messages by request_id so they match properly. This destructuring approach is way cleaner than manually copying properties one by one.

I do this kind of data transformation all the time in n8n. Here’s how I handle it - works with either Code or Function nodes:

// Get your API responses (adjust based on your node setup)
const requestArray = $('HTTP Request 1').all(); // First API call
const messageArray = $('HTTP Request 2').all(); // Second API call

// Extract the actual data
const supportRequest = requestArray[0].json[0]; // First item from first array
const messages = messageArray[0].json; // Full second array

// Build your target structure
const result = {
  "SupportRequest": supportRequest,
  "messages": messages
};

return [{ json: result }];

You’re not really “merging” arrays here - just restructuring data into a new object. Since both arrays have the request_id field, you can add filtering logic to match messages to the right request if you’re processing multiple requests at once. I’ve used this pattern for tons of helpdesk API integrations.