File size: 3,421 Bytes
7fb350b
 
 
 
6c304f1
 
 
c417d52
6c304f1
 
 
 
 
c417d52
 
 
 
 
6c304f1
c417d52
 
 
6c304f1
c417d52
 
 
 
 
6c304f1
c417d52
6c304f1
 
c417d52
6c304f1
 
 
c417d52
6c304f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c417d52
6c304f1
 
 
 
c417d52
6c304f1
c417d52
6c304f1
 
 
c417d52
6c304f1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
---
sidebar_position: 2
---

# 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](../img/script_tab.jpg)

## Code Structure

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

```javascript
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:

```javascript
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:

```javascript
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:

|    Type    |   Time      |     Description       |
|------------|-------------|-----------------------|
| Rapid      | 0-20 sec    | Fast operations       |
| Regular    | 21-60 sec   | Standard processing   |
| Deferred   | 60-120 sec  | Complex operations    |
| Protracted | 120-300 sec | Heavy 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.