n8n中的Javascript:将2个JSON数组合并到一个结构中

b0zn9rqh  于 2023-02-10  发布在  Java
关注(0)|答案(1)|浏览(216)

在一个项目中,我从Zammad-API中取出JSON对象。
我拿到票了:

[
  {
    "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
  }
]

以及属于该票证的票证物品:

[
  {
    "id": 130,
    "ticket_id": 53,
    "type_id": 1,
    "sender_id": 1,
    "from": "Stefan Weichinger <office@my.tld>",
    "to": "info@company1.com",
    "cc": null,
    "subject": "Testanfrage Weichinger",
    "reply_to": null,
    "message_id": "<76d10a5b-64eb-7bfa-98c3-e31ed062802e@my.tld>",
    "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": "office@my.tld",
    "updated_by": "office@my.tld"
  },
  {
    "id": 131,
    "ticket_id": 53,
    "type_id": 1,
    "sender_id": 1,
    "from": "\"Stefan White via company1 Support\" <info@company1.com>",
    "to": "office@my.tld",
    "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": "<76d10a5b-64eb-7bfa-98c3-e31ed062802e@my.tld>",
    "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: hot@example.com - 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": "sel@stefanwhite.com",
    "updated_by": "sel@stefanwhite.com"
  }
]

一个自由职业者写了一个nodejs,我可以通过http-request调用它。
该代码需要将两个JSON对象组合在一个"主体"中,如:

{
"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 <office@my.tld>",
    "to": "info@company1.com",
    "cc": null,
    "subject": "Testanfrage Weichinger",
    "reply_to": null,
    "message_id": "<76d10a5b-64eb-7bfa-98c3-e31ed062802e@my.tld>",
    "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": "office@my.tld",
    "updated_by": "office@my.tld"
  },
  {
    "id": 131,
    "ticket_id": 53,
    "type_id": 1,
    "sender_id": 1,
    "from": "\"Stefan White via company1 Support\" <info@company1.com>",
    "to": "office@my.tld",
    "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": "<76d10a5b-64eb-7bfa-98c3-e31ed062802e@my.tld>",
    "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: hot@example.com - 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": "sel@stefanwhite.com",
    "updated_by": "sel@stefanwhite.com"
  }
  ]
}

在我的n8n工作流中,我可以添加js-code来实现这一点,有人能帮我写这个吗?我在谷歌上搜索了合并和连接json,但是额外添加的"名称"让我更难(添加"Ticket:"和"articles:"...)。到目前为止,我根本不是一个js-coder ...-)
任何帮助感激!谢谢

ozxc1zmp

ozxc1zmp1#

假设你把票存储在一个名为tickets的变量中,把所有的文章存储在一个名为articles的变量中,你可以简单地把票Map到新的组合类型。

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

您可以简单地通过ticket_id过滤每个票证的文章。

相关问题