英文:
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 & 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:
[
{
"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 <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"
}
]
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 <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 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
得分: 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 => ({
Ticket: ticket,
articles: articles.filter(article => article.ticket_id === ticket.id)
}));
You can simply filter the articles for each ticket by the ticket_id.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论