Combining two JSON arrays into a single object in n8n using JavaScript

Need assistance merging JSON data from separate API responses

I’m currently handling an API for a support ticketing system that returns information in multiple parts. One part consists of JSON data for the main ticket, and another includes comments related to that ticket.

Here’s the format of the main ticket data:

[
  {
    "order_id": 789,
    "department_id": 5,
    "urgency_level": 3,
    "status_id": 1,
    "company_id": null,
    "reference": "REF-2024-001",
    "subject": "System Integration Issue",
    "assigned_to": 12,
    "requester_id": 45,
    "description": "Database connection problems",
    "opened_at": "2024-01-10T09:30:15.123Z",
    "due_date": "2024-01-15T17:00:00.000Z",
    "resolution_time": 240,
    "last_activity": "2024-01-12T14:22:30.456Z",
    "tags": ["database", "urgent"],
    "custom_fields": {
      "priority_score": 85,
      "category": "technical"
    }
  }
]

And below is the comments data:

[
  {
    "comment_id": 567,
    "order_id": 789,
    "author_id": 12,
    "author_email": "[email protected]",
    "recipient": "[email protected]",
    "comment_text": "Initial investigation shows connection timeout issues",
    "visibility": "public",
    "posted_at": "2024-01-10T10:15:22.789Z",
    "edited_at": "2024-01-10T10:16:05.234Z",
    "comment_type": "update",
    "author_name": "John Doe"
  },
  {
    "comment_id": 568,
    "order_id": 789,
    "author_id": 45,
    "author_email": "[email protected]",
    "recipient": "[email protected]",
    "comment_text": "The issue started after the recent server update",
    "visibility": "public",
    "posted_at": "2024-01-10T11:45:33.567Z",
    "edited_at": null,
    "comment_type": "reply",
    "author_name": "Jane Smith"
  }
]

I want to merge these into a single object formatted like this:

{
  "MainTicket": {
    "order_id": 789,
    "department_id": 5,
    "urgency_level": 3,
    "status_id": 1,
    "company_id": null,
    "reference": "REF-2024-001",
    "subject": "System Integration Issue",
    "assigned_to": 12,
    "requester_id": 45,
    "description": "Database connection problems",
    "opened_at": "2024-01-10T09:30:15.123Z",
    "due_date": "2024-01-15T17:00:00.000Z",
    "resolution_time": 240,
    "last_activity": "2024-01-12T14:22:30.456Z",
    "tags": ["database", "urgent"],
    "custom_fields": {
      "priority_score": 85,
      "category": "technical"
    }
  },
  "Comments": [
    {
      "comment_id": 567,
      "order_id": 789,
      "author_id": 12,
      "author_email": "[email protected]",
      "recipient": "[email protected]",
      "comment_text": "Initial investigation shows connection timeout issues",
      "visibility": "public",
      "posted_at": "2024-01-10T10:15:22.789Z",
      "edited_at": "2024-01-10T10:16:05.234Z",
      "comment_type": "update",
      "author_name": "John Doe"
    },
    {
      "comment_id": 568,
      "order_id": 789,
      "author_id": 45,
      "author_email": "[email protected]",
      "recipient": "[email protected]",
      "comment_text": "The issue started after the recent server update",
      "visibility": "public",
      "posted_at": "2024-01-10T11:45:33.567Z",
      "edited_at": null,
      "comment_type": "reply",
      "author_name": "Jane Smith"
    }
  ]
}

Could someone provide me with JavaScript code to achieve this in n8n? I’m struggling with how to format it correctly to include sections like “MainTicket” and “Comments”. Any guidance would be greatly appreciated. Thank you!

The Code node in n8n makes this pretty easy once you get the structure down. You’ll grab data from your previous nodes and build the new object format. If your ticket data’s from one node and comments from another, use $input.all() or target specific node outputs. Just pull the first element from your ticket array (looks like it’s a single ticket anyway) and assign both datasets to their keys. Here’s what’s worked for me:

// Ticket data from node 0, comments from node 1
const ticketData = $input.all()[0].json;
const commentsData = $input.all()[1].json;

const mergedResult = {
  MainTicket: ticketData[0], // First ticket from array
  Comments: commentsData     // Comments stay as array
};

return mergedResult;

Just tweak the node references for your workflow. This’ll give you exactly what you need.

If your ticket and comments data are coming from separate inputs in n8n, just handle it in a Code node. You need to understand how n8n passes data between nodes and structure your output correctly.

// Access data from previous nodes
const ticketArray = $input.first().json; // or $('NodeName').first().json
const commentsArray = $input.last().json; // or $('CommentsNode').all().map(item => item.json)

// Create the merged structure
const result = {
  MainTicket: ticketArray[0], // Extract single ticket object
  Comments: commentsArray     // Keep comments as array
};

return [{ json: result }];

Return the data in n8n’s expected format with the json wrapper. I’ve used this for similar API consolidation tasks and it handles the data structure transformation cleanly. Just watch how you’re accessing the input data - whether it’s from multiple nodes or a single node with multiple items.

If you’re using n8n’s code node, this’ll work. Just make sure you’re grabbing the right input data and wrapping your output so n8n can handle it:

const ticketData = $input.first().json;
const commentsData = $input.last().json;

return {
  MainTicket: ticketData[0],
  Comments: commentsData
};

Works great for API merging like this!