Manipulate JSON Array to send to Hubspot API

Hi there,

I’m creating a workflow where I pull the activities from a certain contact in Hubspot.

Once I have all the activity IDs, I’ll pass them to the Read a batch of calls by internal ID, or unique property values API call to retrieve the owner information and manipulate the data later on.

The problem is that I get the IDs like this:

[
 {"inputs": [{
   "id": "18691908716",
   "type": "contact_to_call"
  },
  {
  "id": "18858006081",
  "type": "contact_to_call"
  }
]

And I must send them to Hubspot in this exact format:

"inputs": [
    {
      "id": "1"
    },
    {
      "id": "2"
    }
  ]

I’m using a Function node that seems to do the job with the following code and result:

const oldItems = items.map(it => it.json.inputs).map(it => it.map(iit => iit.id));

const newItems = [];
 
 oldItems.forEach( it => {
   it.forEach(it2 => {
      newItems.push({
      json: {
        id: it2
      }
      });
   });
 });


return newItems;

But when I try to add the values to as an Expression in the API call body, it either retrieves only the first value or comes in a different format that the API won’t accept, as per the following prints:

I need the array to contain values only in the format {"id":"18691908716"},{"id":"18858006081"} and it has to be an Expression or other method that allows me to pass N IDs at the same time.

Is there an easy solution that I’m missing here? Happy to give more details if needed.

Hi @Aleph-marchtein , there’s probably a more elegant and efficient way of achieving what you’re looking for, but this is a little workflow that I made with my limited knowledge (so far) of n8n data transformation. Hope this helps.

1 Like

Hi @Aleph-marchtein in the meantime I’m getting a little up to speed with the Function nodes, and simplified the workflow, as follows:

1 Like

Thanks @dickhoning , your code helped simplify things a bit!

I was still running into an issue when I put the Array in the HTTP node, but them I realized that I had to pass them as a string, so this code solved my problem:

{{JSON.stringify($node["Function1"].json["inputs"])}}

1 Like