Javascript in n8n: combine 2 JSON arrays into one structure

523 Views Asked by At

In a project I pull JSON-objects out of the Zammad-API.

I get the ticket:

[
  {
    "id": 53,
    "group_id": 2,
    "priority_id": 2,
    "state_id": 2,
    "organization_id": null,
    "number": "740534",
    "title": "Testanfrage Weichinger",
    "owner_id": 3,
    "customer_id": 3,
    "note": null,
    "first_response_at": "2022-11-15T07:19:04.078Z",
    "first_response_escalation_at": null,
    "first_response_in_min": 0,
    "first_response_diff_in_min": 4320,
    "close_at": null,
    "close_escalation_at": null,
    "close_in_min": null,
    "close_diff_in_min": null,
    "update_escalation_at": null,
    "update_in_min": null,
    "update_diff_in_min": null,
    "last_contact_at": "2022-11-15T07:20:05.013Z",
    "last_contact_agent_at": "2022-11-15T07:19:41.008Z",
    "last_contact_customer_at": "2022-11-15T07:20:05.013Z",
    "last_owner_update_at": "2022-11-15T12:08:48.875Z",
    "create_article_type_id": 1,
    "create_article_sender_id": 1,
    "article_count": 3,
    "escalation_at": null,
    "pending_time": null,
    "type": null,
    "time_unit": null,
    "preferences": {
      "channel_id": 3,
      "escalation_calculation": {
        "first_response_at": "2022-11-15T07:19:04.078Z",
        "last_update_at": "2022-11-15T07:20:05.013Z",
        "last_contact_at": "2022-11-15T07:20:05.013Z",
        "sla_id": 2,
        "sla_updated_at": "2023-01-11T16:02:47.791Z",
        "calendar_id": 2,
        "calendar_updated_at": "2023-01-15T13:20:30.043Z",
        "escalation_disabled": false
      }
    },
    "updated_by_id": 3,
    "created_by_id": 3,
    "created_at": "2022-11-15T07:19:04.022Z",
    "updated_at": "2023-01-16T07:40:29.206Z",
     "last_close_at": null
  }
]

and the ticket-articles belonging to that ticket:

[
  {
    "id": 130,
    "ticket_id": 53,
    "type_id": 1,
    "sender_id": 1,
    "from": "Stefan Weichinger <[email protected]>",
    "to": "[email protected]",
    "cc": null,
    "subject": "Testanfrage Weichinger",
    "reply_to": null,
    "message_id": "<[email protected]>",
    "message_id_md5": "2bf547f5f570f6a8143ca8084110aee2",
    "in_reply_to": null,
    "content_type": "text/plain",
    "references": null,
    "body": "\nZum Test von Zammad-Emailing.\n\n-- \nViele Grüße | Stefan Weichinger | DW:  | Mobil: \n",
    "internal": false,
    "preferences": {
      "send-auto-response": true,
      "is-auto-response": false
    },
    "updated_by_id": 3,
    "created_by_id": 3,
    "origin_by_id": null,
    "created_at": "2022-11-15T07:19:04.078Z",
    "updated_at": "2022-11-15T07:19:04.078Z",
    "attachments": [],
    "type": "email",
    "sender": "Agent",
    "created_by": "[email protected]",
    "updated_by": "[email protected]"
  },
  {
    "id": 131,
    "ticket_id": 53,
    "type_id": 1,
    "sender_id": 1,
    "from": "\"Stefan White via company1 Support\" <[email protected]>",
    "to": "[email protected]",
    "cc": "",
    "subject": "Testanfrage Weichinger",
    "reply_to": null,
    "message_id": "<20221115071941.53.6087c1df-aa4c-4cbf-b79a-e2498239612b@ticket.company1.com>",
    "message_id_md5": "07a9eee59e351c3d068badce3a56c7a0",
    "in_reply_to": "<[email protected]>",
    "content_type": "text/html",
    "references": null,
    "body": "KLappt nun das ANtworten?<br><br><div data-signature=\"true\" data-signature-id=\"1\">  Stefan White<br><br>--<br> Super Support - Waterford Business Park<br> 5201 Blue Lagoon Drive - 8th Floor &amp; 9th Floor - Miami, 33126 USA<br> Email: [email protected] - Web: <a href=\"http://www.example.com/\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">http://www.example.com/</a><br>--</div>",
    "internal": false,
    "preferences": {
      "subtype": "reply",
      "email_address_id": 1,
      "delivery_retry": 1,
      "delivery_channel_id": 3,
      "delivery_status_message": null,
      "delivery_status": "success",
      "delivery_status_date": "2022-11-15T07:19:43.621Z"
    },
    "updated_by_id": 4,
    "created_by_id": 4,
    "origin_by_id": null,
    "created_at": "2022-11-15T07:19:41.008Z",
    "updated_at": "2022-11-15T07:19:43.622Z",
    "attachments": [],
    "type": "email",
    "sender": "Agent",
    "created_by": "[email protected]",
    "updated_by": "[email protected]"
  }
]

A freelancer has written a piece of nodejs that I can call via http-request.

That code needs both JSON-objects combined in one "body", like in:

{
"Ticket": {
    "id": 53,
    "group_id": 2,
    "priority_id": 2,
    "state_id": 2,
    "organization_id": null,
    "number": "740534",
    "title": "Testanfrage Weichinger",
    "owner_id": 3,
    "customer_id": 3,
    "note": null,
    "first_response_at": "2022-11-15T07:19:04.078Z",
    "first_response_escalation_at": null,
    "first_response_in_min": 0,
    "first_response_diff_in_min": 4320,
    "close_at": null,
    "close_escalation_at": null,
    "close_in_min": null,
    "close_diff_in_min": null,
    "update_escalation_at": null,
    "update_in_min": null,
    "update_diff_in_min": null,
    "last_contact_at": "2022-11-15T07:20:05.013Z",
    "last_contact_agent_at": "2022-11-15T07:19:41.008Z",
    "last_contact_customer_at": "2022-11-15T07:20:05.013Z",
    "last_owner_update_at": "2022-11-15T12:08:48.875Z",
    "create_article_type_id": 1,
    "create_article_sender_id": 1,
    "article_count": 3,
    "escalation_at": null,
    "pending_time": null,
    "type": null,
    "time_unit": null,
    "preferences": {
      "channel_id": 3,
      "escalation_calculation": {
        "first_response_at": "2022-11-15T07:19:04.078Z",
        "last_update_at": "2022-11-15T07:20:05.013Z",
        "last_contact_at": "2022-11-15T07:20:05.013Z",
        "sla_id": 2,
        "sla_updated_at": "2023-01-11T16:02:47.791Z",
        "calendar_id": 2,
        "calendar_updated_at": "2023-01-15T13:20:30.043Z",
        "escalation_disabled": false
      }
    },
    "updated_by_id": 3,
    "created_by_id": 3,
    "created_at": "2022-11-15T07:19:04.022Z",
    "updated_at": "2023-01-16T07:40:29.206Z",
    "datev_dokumentennummer": "",
    "last_close_at": null
  },
  "articles": [
  {
    "id": 130,
    "ticket_id": 53,
    "type_id": 1,
    "sender_id": 1,
    "from": "Stefan Weichinger <[email protected]>",
    "to": "[email protected]",
    "cc": null,
    "subject": "Testanfrage Weichinger",
    "reply_to": null,
    "message_id": "<[email protected]>",
    "message_id_md5": "2bf547f5f570f6a8143ca8084110aee2",
    "in_reply_to": null,
    "content_type": "text/plain",
    "references": null,
    "body": "\nZum Test von Zammad-Emailing.\n\n-- \nViele Grüße | Stefan Weichinger | DW:  | Mobil: \n",
    "internal": false,
    "preferences": {
      "send-auto-response": true,
      "is-auto-response": false
    },
    "updated_by_id": 3,
    "created_by_id": 3,
    "origin_by_id": null,
    "created_at": "2022-11-15T07:19:04.078Z",
    "updated_at": "2022-11-15T07:19:04.078Z",
    "attachments": [],
    "type": "email",
    "sender": "Agent",
    "created_by": "[email protected]",
    "updated_by": "[email protected]"
  },
  {
    "id": 131,
    "ticket_id": 53,
    "type_id": 1,
    "sender_id": 1,
    "from": "\"Stefan White via company1 Support\" <[email protected]>",
    "to": "[email protected]",
    "cc": "",
    "subject": "Testanfrage Weichinger",
    "reply_to": null,
    "message_id": "<20221115071941.53.6087c1df-aa4c-4cbf-b79a-e2498239612b@ticket.company1.com>",
    "message_id_md5": "07a9eee59e351c3d068badce3a56c7a0",
    "in_reply_to": "<[email protected]>",
    "content_type": "text/html",
    "references": null,
    "body": "KLappt nun das ANtworten?<br><br><div data-signature=\"true\" data-signature-id=\"1\">  Stefan White<br><br>--<br> Super Support - Waterford Business Park<br> 5201 Blue Lagoon Drive - 8th Floor &amp; 9th Floor - Miami, 33126 USA<br> Email: [email protected] - Web: <a href=\"http://www.example.com/\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">http://www.example.com/</a><br>--</div>",
    "internal": false,
    "preferences": {
      "subtype": "reply",
      "email_address_id": 1,
      "delivery_retry": 1,
      "delivery_channel_id": 3,
      "delivery_status_message": null,
      "delivery_status": "success",
      "delivery_status_date": "2022-11-15T07:19:43.621Z"
    },
    "updated_by_id": 4,
    "created_by_id": 4,
    "origin_by_id": null,
    "created_at": "2022-11-15T07:19:41.008Z",
    "updated_at": "2022-11-15T07:19:43.622Z",
    "attachments": [],
    "type": "email",
    "sender": "Agent",
    "created_by": "[email protected]",
    "updated_by": "[email protected]"
  }
  ]
}

In my n8n-workflow I can add js-code to do that, could someone help me writing this? I googled around merging and concatenating json, but the additional adding of the "names" makes it harder for me (adding "Ticket:" and "articles:" ...). I am not a js-coder at all ... so far ;-)

Any help appreciated! thanks

1

There are 1 best solutions below

1
On

Assuming you have your tickets stored in some variable named tickets and all your articles stored in some variable named articles. You can simply map your tickets to your new combined type as such.

let cominedTickes = tickets.map(ticket => ({
  Ticket: ticket,
  articles: articles.filter(article => article.ticket_id === ticket.id)
}));

You can simply filter the articles for each ticket by the ticket_id.