How to pass array string from aggregate node to input JSON Expression

I have a JSON from aggregate node like this which has array string

[{“usernames”: [“_ablehhehee”,“gudangcbr”,“toprent.vespa”,“rentalbike.id”,“toprent.motosports”,“transgomotor.rent”,“centermotors.id”,“centermotors.jakarta”,“toprentmotor”,“ittiba_salaf”,“motoxpert.id”,“_lelanginaja”,“rifato”,“garasimotor2022”,“layzmotor”,“andrelyanica”,“motorbekas.jakarta”,“tjmoge_id”,“jktinfo”,“riple.motorental”]}]

and then I want to pass those values to input JSON expression on the next node like this

{
“includeAboutSection”: false,
“usernames”: // I want the array string in here
}

I have tried like this

{
“includeAboutSection”: false,
“usernames”: {{$json.usernames}}
}

but I have error like this

Could not parse custom body: { “includeAboutSection”: false, “usernames”: _ablehhehee,gudangcbr,toprent.vespa,rentalbike.id,toprent.motosports,transgomotor.rent,centermotors.id,centermotors.jakarta,toprentmotor,ittiba_salaf,motoxpert.id,_lelanginaja,rifato,garasimotor2022,layzmotor,andrelyanica,motorbekas.jakarta,tjmoge_id,jktinfo,riple.motorental }

1 Like

Hi @Agung_Laksana Welcome!
You can use Code Node and use javascript to handle this by:

const allUsernames = [];

for (const item of items) {
  if (Array.isArray(item.json.usernames)) {
    allUsernames.push(...item.json.usernames);
  }
}

return [
  {
    json: {
      includeAboutSection: false,
      usernames: allUsernames
    }
  }
];

This is according to the JSON you have shared, and this would work, let me know if this helps.

add .toJsonString():

{{ $json.usernames.toJsonString() }}
1 Like

Replace your current body with this:
{
“includeAboutSection”: false,
“usernames”: {{ JSON.stringify($json.usernames) }}
}

With JSON.stringify: n8n outputs: ["_ablehhehee", "gudangcbr"...] (Valid JSON)

Root Cause:

  1. When an n8n expression like {{ $json.usernames }} is placed inside a text field (like the “Body” of an HTTP Request), n8n default behavior is to convert the object/array into a comma-separated string to make it “readable.”
  2. The output shows “usernames”: _ablehhehee...—the missing quotes around the names and the missing [] around the whole list make the JSON invalid.