Zum Hauptinhalt springen

Script Tab in Node Settings

The Script tab is designed for placing JavaScript code that handles the node's programmatic logic. Here you define what exactly your node does - whether it processes text, calls APIs, transforms data, or performs calculations.

Script tab interface

Code Structure

All Piper nodes, including custom ones, follow a specific JavaScript structure:

export async function run({ inputs }) {
const { FatalError, NextNode } = DEFINITIONS;

// Your code here

return NextNode.from({
outputs: {
// Your results
}
});
}

This structure ensures that your node can properly receive data from previous nodes and pass results to the next ones in your pipeline.

Original Input Text Code

The basic Input Text node has this simple structure:

export async function run({ inputs }) {
const { FatalError, NextNode } = DEFINITIONS;

const text = inputs.input_text;

return NextNode.from({
outputs: {
output_text: text
}
});
}

Custom Merge Text to JSON Code

We need to replace this with logic that processes multiple string inputs and creates a JSON array:

export async function run({ inputs }) {
const { FatalError, NextNode } = DEFINITIONS;

const jsonArray = [];

if (inputs.string1) {
jsonArray.push(inputs.string1);
}
if (inputs.string2) {
jsonArray.push(inputs.string2);
}
if (inputs.string3) {
jsonArray.push(inputs.string3);
}
if (inputs.string4) {
jsonArray.push(inputs.string4);
}

return NextNode.from({
outputs: {
json_output: jsonArray
}
});
}

Understanding Custom Logic

The custom script implements the following logic:

  1. Initialize empty array - const jsonArray = [] creates an empty array to store our text strings
  2. Check each input - if conditions check that each input has a value before adding
  3. Build array - Only non-empty inputs are added to jsonArray using push()
  4. Return JSON output - The final array is returned as json_output, matching our configuration in the Design tab

This approach ensures that:

  • Empty or undefined inputs are ignored
  • Only valid text strings are included in the final JSON array
  • The output format matches the JSON type we defined in the Design tab

Execution Types

Choose the appropriate execution type based on how much time your node requires to work:

TypeTimeDescription
Rapid0-20 secFast operations
Regular21-60 secStandard processing
Deferred60-120 secComplex operations
Protracted120-300 secHeavy processing tasks

For our Merge text to JSON node, Rapid execution is suitable since we're only manipulating text strings and there are no API calls to external services.

Saving Changes

Always save changes to the script by clicking the Save button at the bottom of the interface. Any changes to the code should be saved by pressing the Save button at the bottom of the interface.

The Script tab is the core of your node's functionality, where you transform input data into output using JavaScript code.