Wie man ein einzelnes Schlüssel-Wert-Paar aus dem Eingabeelement extrahiert, es ändert und es in die Ausgabe des Code-Knotens zurückgibt?

Hello, Everyone.

I’m struggling with processing input data in the Code node (taken from a n8n Form). I have a following input:

[

{

“Imię i nazwisko:”: “dfgdg”,

“e-mail:”: “gdg@dfdf.gdf”,

“Telefon:”: “4453453”,

“Bierzesz udział jako:”: “Firma”,

“submittedAt”: “2026-06-18T17:04:14.285+02:00”,

“formMode”: “test”

}

]

I want to check if the value of the field “Telefon:” has whitespaces and if so, to remove them, and next to put modified input to the output, with some extra fields created in the Code node. The problem I have is that I can’t put this modified input in the output of the node.

Here is my workflow, and the node I have problem with, is called “Calculate dates and NIP”.

Hallo, das Hauptproblem ist, dass die Ausgabe des Code-Knotens als ein Element mit einem json-Objekt zurückgegeben werden muss.

Außerdem musst du nicht durch die Telefonnummer loopen, um Leerzeichen zu entfernen. Du kannst replace(/\s+/g, "") verwenden.

Versuche dieses Muster in deinem Calculate dates and NIP Code-Knoten:

const input = $input.first().json;

const cleanedPhone = String(input["Telefon:"] ?? "").replace(/\s+/g, "");

const availableDates = Array.from({ length: 6 }, (_, offset) => {
  const dayOffset = (today) => 2 - today;
  const date = $now.plus({
    weeks: offset + 1,
    days: dayOffset(DateTime.local().weekday),
  });

  return {
    option: date.toFormat("dd.MM.yyyy"),
  };
});

const formFields = [
  {
    fieldLabel: "Wybierz termin",
    fieldType: "dropdown",
    requiredField: true,
    fieldOptions: {
      values: availableDates,
    },
  },
];

const nipInput =
  input["Bierzesz udział jako:"] === "Firma"
    ? [
        {
          fieldLabel: "Podaj NIP",
          fieldType: "text",
          requiredField: true,
        },
      ]
    : [];

return [
  {
    json: {
      ...input,
      "Telefon:": cleanedPhone,
      combinedFormFields: [...formFields, ...nipInput],
    },
  },
];

Das behält deine ursprünglichen Formulardaten bei, aktualisiert Telefon: und fügt combinedFormFields für den nächsten Form-Knoten hinzu.

Hilfreiches Nachschlagewerk: n8n-Dokumentation zur Datenstruktur

1 „Gefällt mir“

Hallo Imad,

Danke für deine Hilfe. Es ist mir immer noch nicht ganz klar, wie ich dieses Output-Objekt richtig aufbaue. Deine Lösung funktioniert perfekt!