Skip to main content
Version: 1.2.0

Slice

Array Operations Data Extraction

Synopsis

A processor that extracts a portion of an array field by specifying start and end indices. The processor supports negative indexing and automatically handles various array types including strings, integers, and floats.

Schema

- slice:
field: <ident>
start: <number>
end: <number>
target_field: <ident>
description: <text>
if: <script>
ignore_failure: <boolean>
ignore_missing: <boolean>
on_failure: <processor[]>
on_success: <processor[]>
tag: <string>

Configuration

The following fields are used to define the processor:

FieldRequiredDefaultDescription
fieldY-Array field to slice
startY-Starting index (inclusive)
endY-Ending index (inclusive)
target_fieldNfieldField to store the sliced array
descriptionN-Explanatory note
ifN-Condition to run processor
ignore_failureNfalseContinue if processor fails
ignore_missingNfalseContinue if source field doesn't exist
on_failureN-See Handling Failures
on_successN-See Handling Success
tagN-Identifier

Details

The processor extracts a contiguous portion of an array using zero-based indexing. Both start and end indices are inclusive, meaning the element at the end index is included in the result.

note

The processor supports negative indexing, where -1 refers to the last element, -2 to the second-to-last, and so on.

The processor automatically converts different array types ([]string, []int, []float64) to a unified []any format for processing. If the source field is not an array, the processor will fail unless ignore_failure is set to true.

Index bounds are automatically normalized to prevent out-of-bounds errors. If start index is greater than end index after normalization, an empty array is returned.

warning

When start index is greater than or equal to array length, an empty array is returned. Similarly, negative indices that extend beyond the array bounds are clamped to valid ranges.

Examples

Basic Array Slicing

Extracting middle elements...

{
"numbers": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
}
- slice:
field: numbers
start: 2
end: 5
target_field: subset

extracts elements from index 2 to 5:

{
"numbers": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
"subset": [3, 4, 5, 6]
}

Negative Indexing

Using negative indices...

{
"items": ["apple", "banana", "cherry", "date", "elderberry"]
}
- slice:
field: items
start: -3
end: -1
target_field: last_three

extracts the last three elements:

{
"items": ["apple", "banana", "cherry", "date", "elderberry"],
"last_three": ["cherry", "date", "elderberry"]
}

First N Elements

Getting first few elements...

{
"logs": ["error1", "warn1", "info1", "error2", "warn2", "info2"]
}
- slice:
field: logs
start: 0
end: 2
target_field: first_three

extracts first three elements:

{
"logs": ["error1", "warn1", "info1", "error2", "warn2", "info2"],
"first_three": ["error1", "warn1", "info1"]
}

Single Element Extraction

Extracting single element...

{
"scores": [85, 92, 78, 95, 88]
}
- slice:
field: scores
start: 1
end: 1
target_field: second_score

extracts single element as array:

{
"scores": [85, 92, 78, 95, 88],
"second_score": [92]
}

Mixed Type Arrays

Processing different array types...

{
"temperatures": [20.5, 21.2, 19.8, 22.1, 20.9]
}
- slice:
field: temperatures
start: -2
end: -1
target_field: recent_temps

handles float arrays:

{
"temperatures": [20.5, 21.2, 19.8, 22.1, 20.9],
"recent_temps": [22.1, 20.9]
}

In-Place Modification

Modifying original field...

{
"data": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
}
- slice:
field: data
start: 3
end: 7

replaces original field content:

{
"data": [4, 5, 6, 7, 8]
}

Out-of-Bounds Handling

Handling invalid indices...

{
"small_array": ["a", "b", "c"]
}
- slice:
field: small_array
start: 5
end: 10
target_field: result

returns empty array for out-of-bounds:

{
"small_array": ["a", "b", "c"],
"result": []
}