I’m working with an API that provides customer support information across two distinct JSON arrays. I need to merge these arrays into one structured object to use in my workflow.
The first array includes order details:
[
{
"order_id": 125,
"department_id": 1,
"status_id": 3,
"client_id": 15,
"reference_number": "ORD-2024-001",
"description": "Product inquiry from customer",
"assigned_to": 7,
"customer_id": 15,
"notes": null,
"created_date": "2024-01-20T09:30:15.123Z",
"due_date": null,
"completion_time": 0,
"processing_minutes": 2880,
"completed_at": null,
"category": "inquiry",
"priority_level": "normal",
"last_activity": "2024-01-20T10:15:22.456Z",
"agent_response_at": "2024-01-20T09:45:30.789Z",
"customer_last_contact": "2024-01-20T10:15:22.456Z",
"settings": {
"source_channel": 1,
"workflow_data": {
"initial_response": "2024-01-20T09:45:30.789Z",
"last_modified": "2024-01-20T10:15:22.456Z",
"auto_close": false
}
},
"modified_by": 7,
"created_by": 7,
"created_timestamp": "2024-01-20T09:30:15.100Z",
"updated_timestamp": "2024-01-20T14:20:10.500Z"
}
]
The second array contains corresponding messages:
[
{
"message_id": 250,
"order_id": 125,
"category_id": 2,
"author_id": 1,
"sender_email": "john.doe <[email protected]>",
"recipient": "[email protected]",
"subject_line": "Product inquiry from customer",
"thread_id": "<[email protected]>",
"content": "Hello, I need information about your services.",
"is_private": false,
"meta_info": {
"auto_reply_sent": true,
"is_automated": false
},
"last_edited_by": 7,
"author_user_id": 7,
"message_type": "email",
"sender_role": "Customer",
"created_timestamp": "2024-01-20T09:45:30.789Z",
"modified_timestamp": "2024-01-20T09:45:30.789Z"
}
]
I want to create this combined structure:
{
"OrderData": {
"order_id": 125,
"department_id": 1,
// ... other order fields
},
"Messages": [
{
"message_id": 250,
"order_id": 125,
// ... other message fields
}
]
}
I’m seeking assistance in writing the JavaScript code for n8n that can merge these arrays and assigns the appropriate field names. I’m not well-versed in JavaScript, so I could use some guidance on how to structure this merge operation.