Using Javascript in n8n to merge two JSON arrays into a single data structure

When working on a project, I extract JSON objects from the Zammad-API.

One of the tickets retrieved is as follows:

[
  {
    "id": 53,
    "group_id": 2,
    "priority_id": 2,
    "state_id": 2,
    "organization_id": null,
    "number": "740534",
    "title": "Testanfrage Weichinger",
    ...
  }
]

Additionally, there are ticket articles associated with this ticket:

[
  {
    "id": 130,
    "ticket_id": 53,
    "type_id": 1,
    ...
  },
  {
    "id": 131,
    "ticket_id": 53,
    "type_id": 1,
    ...
  }
]

A freelancer has provided a Node.js script that can be accessed via an HTTP request.

This code requires both JSON objects to be combined in one "body" like so:

{
"Ticket": {
    "id": 53,
    "group_id": 2,
    "priority_id": 2,
    "state_id": 2,
    ...
  },
  "articles": [
  {
    "id": 130,
    "ticket_id": 53,
    "type_id": 1,
    ...
  },
  {
    "id": 131,
    "ticket_id": 53,
    "type_id": 1,
    ...
  }
  ]
}

I need help writing a JavaScript code snippet for my n8n workflow to achieve this combination. I have tried searching for solutions on merging and concatenating JSON, but adding the necessary names like "Ticket:" and "articles:" complicates things for me as I am not familiar with JS coding. Any assistance would be greatly appreciated!

Answer №1

If you have a list of tickets saved in a variable called tickets and a list of articles saved in a variable called articles, you can easily connect them by mapping the tickets to the combined type below.

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

To associate each ticket with its corresponding articles, simply filter the articles by matching their ticket_id.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Include an external JavaScript file within a component to display pictures in a web browser using Angular 2

I am developing a website using Angular 2 and need to incorporate a JavaScript file into a component. The goal is for this script to adjust the height of certain images to match the height of the browser window. What is the best way to integrate this scri ...

Experiencing challenges in integrating fundamental Javascript elements on a chat page

My chat page skeleton is in place, but I'm facing challenges in connecting all the pieces. My goal is to send messages to the server whenever a user clicks 'send', and to update the displayed messages every 3 seconds. Any insights, tips, or ...

Enhance the table using Django URL tag along with JQuery

I am currently working on a table that is being populated with user details and I would like to include a Django URL tag within the row, extracting the primary key in the process. Here is an example of what I am trying to achieve: function putTableData(re ...

Why doesn't the z-index of child elements function properly when their fixed parents share the same z-index value?

I have utilized jsfiddle to replicate my problem. My goal is to position .top .inside above .bottom .inside. I am aware that z-index only functions within its respective position type, for example fixed and absolute do not share the same z-index level. How ...

What is the best way to implement a personalized hook in React that will return the number of times a specific key is pressed?

I'm working on a custom hook that should give me the key pressed, but I've noticed that when I press the same key more than twice, it only registers twice. Here's my code: import { useEffect, useState } from "react" function useKe ...

Converting JSON data to XML format using Laravel

I am looking for a way to convert JSON format to XML using Laravel. Here is the expected output: <MESSAGE> <AUTHKEY>Your auth key</AUTHKEY> <SENDER>SenderID</SENDER> <ROUTE>Template</ROUTE> <CAM ...

Step-by-step guide for combining two SQL rows into a PHP array

I need to retrieve two values from a database - a path and a tag - and add them to an array. Then, I plan to encode the array to display the image and dynamically order the new javascript array based on the tags. In the end, I want to use "echo $data;" t ...

Capturing the value of an input field within a controller in AngularJS

I have been programming with JSP and Angular JS. Currently, I am faced with a scenario where I need to work with a JSP page containing a hidden input field. To set the value of this input field, I am using a session attribute like so: String policy = (S ...

Utilize Vue to call a method by providing its name as a string

When designing a navbar, I encountered the need to include a list of buttons. Some of these buttons should act as links, while others should call specific methods. For example, clicking on "Home" should direct the user to /home and clicking on "Log out" sh ...

Tips for enabling multiple v-list-group components to remain open simultaneously (bypassing the default Vue behavior)

Currently, I am facing an issue with Vue's default behavior where only one v-list-group can be open at a time. I am using Vuetify 2.6 and have attempted to use the multiple prop without success. Additionally, individually assigning the :value prop to ...

"Encountering errors during npm installation due to a failed preinstall

Having identified security vulnerabilities for knexnest > knex > minimist, I encountered an issue where the minimist version did not update using npm audit fix or a simple npm update. Following the steps outlined in this informative article resolved ...

Tips on displaying a particular JSON attribute?

After starting with a JSON string, attempting to convert it into a JSON object and then trying to print a specific field (such as firstName), I am getting undefined. What could be the issue here? Thank you for your help! var string = '{"firstName ...

Elements animated using intersection observer are inconsistently functioning on the current page

My current challenge involves animating elements with the intersection observer, but I am encountering strange behavior with elements that are already on the screen when the page loads. These elements sometimes animate correctly and other times they do no ...

When attempting to use a value outside of its block, the function may return a

My current task involves querying one collection to retrieve IDs, then using those IDs to query another collection and send back the response. The process runs smoothly until I encounter an issue with retrieving values outside of a block when using forEach ...

Simulated web server for testing with Jest

Can I ask a unique question? I have a tool for extracting data from webpages directly, not through APIs. I want to create end-to-end tests for this tool using the Jest library, but I need to ensure the web pages I'm referencing remain consistent. It&a ...

Is there a way to identify and count duplicate data-items within an array, and then showcase this information using a react view?

If we have an array like this: [{id: 1, name: "chocolate bar"}, {id:2, name: "Gummy worms"}, {id:3, name:"chocolate bar"}] Is there a way to show on the dom that we have "2 chocolate bars and 1 Gummy Worms"? ...

Employ material-ui default prop conditionally

I am implementing a StepLabel component in material ui. Depending on the props passed to the parent, I may need to make changes to the icon property of the StepLabel: interface Props { customClasses?: ClassNameMap; customIcon?: ReactNode; } const MySt ...

retrieving multiple values in ajax call and processing them in controller function

I'm facing a challenge in Laravel when it comes to sending multiple input options (generated by a foreach loop) through an ajax call. The goal is to send multiple values and loop through them in the ajax call to effectively pass them to the controller ...

Achieving CommonJS imports compilation with Typescript

In my TS file, I've included a 3rd party package using import XXX { YYY, ABC, 123 } from 'XXX'; While it compiles to CommonJS without any issues, I'd prefer to have it compiled to an ESModule instead. I tried changing the target and mo ...

Using VueJs's createElement() to dynamically insert HTML content

I am currently working on a component that converts all icons to SVG format. By the end of my code, I have included the following: return createElement('i', '<SVG>CODE</SVG>' ) In the spot where the SPA ...