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

Interactive JQuery calendar

Can anybody assist me with this issue? I am seeing question marks in the graphic and I'm not sure why. I remember encountering these symbols before and I believe it has something to do with charset. Currently, I am using: <meta http-equiv="Content ...

NodeJS - session information lost after building ReactJavaScript application

I've created a NodeJS web application with basic functionality for user login and pulling data from MySql based on session information. The app is functioning as intended (Link). However, when I implement the client-side in React, the session data is ...

Transforming dataframe into JSON using R

Attempting to convert the given dataframe into JSON has proven to be a challenging task for me. Despite my efforts to seek guidance online, it seems no one has successfully accomplished this with an r dataframe. df<-data.frame(profileKey=c(7,7,7,8,8,8) ...

Monitoring user logins based on user identification

How can I effectively monitor the activity of users who have logged into a web application that is managed by an external company? Despite my extensive research efforts, as a non-technical individual, I am struggling to understand how to utilize Google Ana ...

Using JavaScript to parse JSON data generated by PHP using an echo statement may result in an error, while

I am facing an issue with parsing a JSON string retrieved from PHP when the page loads. It's strange that if I send an AJAX request to the same function, the parsing is successful without any errors. The problem arises when I attempt to do this: JSON ...

When switching windows or tabs, the user interface of the browser extension vanishes

As someone who is new to web application development and browser extension creation, I have encountered a challenge with my browser extension. When the extension popup is open and I switch browser windows, the UI (popup.html) disappears. It reappears whe ...

Displaying a div when a radio button is clicked in React

I am attempting to create a functionality where clicking on one radio button will display another div in React. As a beginner in React, I have tried implementing the code below but encountered issues with hiding and displaying the content based on user inp ...

Effective method for obtaining the URL from a Node.js application

I'm curious if there is a method to extract a url such as http://whatever:3000/somemethod/val1/val2/val3 Is there an alternative to using .split after obtaining the path name? For example, I attempted to acquire /somemethod/val1/val2/val3 and then ...

Tips for receiving dual return values from an AJAX request

I am sending an array of table IDs to retrieve the table numbers associated with those IDs from the database. I need to add up all the default seats for each table ID and return the total. JAVASCRIPT : function showUser(str) { if ...

Is there a way to tally the frequency of a specific property appearing in one array within another, and then transfer those matches into a separate array?

My goal is to match the fk_city with the $id of each city in the 'list_cities' array, and then calculate the number of occurrences. const list_cities = [ { $id: '15FG', name: 'Pittsburg' }, { $id: '50HS', name: & ...

How to include a javascript file in a different file and compile it using Node.js

For running my practice JS files, I rely on Node. As an example, I use node bts.js. In order to implement a queue, I decided to install Collections by using the command npm install collections --save. You can also see this reflected in the hierarchy shown ...

Configure webpack to source the JavaScript file locally instead of fetching it through HTTP

Using webpack.config.js to fetch remote js for Module Federation. plugins: [ new ModuleFederationPlugin({ remotes: { 'mfe1': "mfe1@http://xxxxxxxxxx.com/remoteEntry.js" } }) ], Is it possible to incorporate a local JS ...

Searching through multiple columns in datatables

I encountered an issue with searching multiple columns in my serverside datatables. Individual column searches work fine, but when trying to search on more than one column simultaneously, the filter does not function as expected. For example, searching by ...

Using Typescript to Import One Namespace into Another Namespace

Is it possible to export a namespace from one typescript .d.ts file and then import that namespace into another .d.ts file where it is utilized inside of a namespace? For instance: namespace_export.d.ts export namespace Foo { interface foo { ...

The PHP script is not receiving any data when checking if the value is set in the $_POST variable

I'm attempting to transmit values from a JavaScript file using POST method to a PHP page. Here is the AJAX code: let userInput = $input.val(); $.ajax({url: "../checkout/test.php", type : 'post', data : {'userInput': user ...

Handling OnClick events in D3 with Websocket Integration

My goal is to implement a Websocket in JavaScript that transmits a variable obtained when clicking on a node in a D3 chart. While I have made progress using static data, I'm struggling with initiating the code upon node click to retrieve the "user inf ...

Error: The variable "Tankvalue" has not been declared

Is there a way to fix the error message I am receiving when trying to display response data in charts? The Tankvalue variable seems to be out of scope, resulting in an "undefined" error. The error message states that Tankvalue is not defined. I need to ...

Tips for utilizing the "Sign In with Apple" feature through Apple JS

For implementing "Sign In with Apple" on a web platform using Apple JS, you can refer to the code example available at this link. Now, the query arises: where can I find the Client ID required for this process? I have tried using the app id identifier fro ...

In JavaScript, the task involves filtering through multiple arrays of objects to calculate the average value based on the contract number

Every object in the Array contains a contractNumber, but with repeated values. I am attempting to determine the average value of each unique contractNumber. var vendorArr=[{ "contractNumber":5258, "monthId":0, "value":2}, { ...

Identifying the Click Event Within an ngx Bootstrap Modal

I recently set up an ngx bootstrap modal using the instructions provided in this helpful guide - . However, I'm facing a challenge in detecting click events within the modal body once it's open. Below is the code snippet from my app component. D ...