Javascript在n8n中:将2个JSON数组合并为一个结构

huangapple go评论209阅读模式
英文:

Javascript in n8n: combine 2 JSON arrays into one structure

问题

{
"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"
  }
  ]
}
英文:

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

I get the ticket:

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

and the ticket-articles belonging to that ticket:

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

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:

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

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 Javascript在n8n中:将2个JSON数组合并为一个结构

Any help appreciated! thanks

答案1

得分: 1

假设您将门票存储在名为 tickets 的变量中,将所有文章存储在名为 articles 的变量中。您可以如此简单地将您的门票映射到您的新组合类型中。

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

您可以通过 ticket_id 来简单地筛选每张门票的文章。

英文:

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 =&gt; ({
Ticket: ticket,
articles: articles.filter(article =&gt; article.ticket_id === ticket.id)
}));

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

huangapple
  • 本文由 发表于 2023年2月8日 15:55:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定