{jsonarray:text:delimiter}

Description

Splits a delimited string and returns its non-empty pieces as a JSON array. The default delimiter is a hyphen; pass a second argument to use another separator (a single space is allowed, because the arguments are not trimmed). Empty pieces are dropped. The input must be UTF-8. Note that if an empty piece is removed from the start or the middle, the surviving pieces keep their original positions and the result is a JSON object keyed by those positions rather than an array - a trailing empty piece is safe. Use it to feed a delimited field value into JavaScript or a JSON parameter.

Parameters

text required default (none - required)

The delimited string to convert. It is split on the delimiter and each non-empty piece becomes one array element. Must be UTF-8. Whitespace is not trimmed.

delimiter optional default - (hyphen)

The separator used to split the text. Parameters are not trimmed, so a single space is a valid delimiter.

Examples

test{jsonarray:one-two-three}
Expected["one","two","three"]
Actual["one","two","three"]
The default delimiter is a hyphen. Each non-empty piece becomes one string element of the JSON array.
test{jsonarray:hello}
Expected["hello"]
Actual["hello"]
With no delimiter present the whole input is one element, so the result is a one-element array.
test{jsonarray:apple,pear,plum:,}
Expected["apple","pear","plum"]
Actual["apple","pear","plum"]
Pass the delimiter as the second parameter. Here a comma splits the list instead of the default hyphen.
test{jsonarray:red green blue: }
Expected["red","green","blue"]
Actual["red","green","blue"]
jsonarray does not trim its parameters, so a single space is a valid delimiter and the pieces keep no extra whitespace here.
test{jsonarray:one-two-}
Expected["one","two"]
Actual["one","two"]
A trailing empty piece simply drops out and the result stays a normal sequential array. (An empty piece at the start or in the middle behaves differently - see the next example.)
test{jsonarray:one--three}
Expected{"0":"one","2":"three"}
Actual{"0":"one","2":"three"}
Watch out: an empty piece in the MIDDLE is also dropped, but the remaining pieces keep their original positions (0 and 2). A JSON array cannot have a gap, so the result becomes a JSON object keyed by those positions, not an array. Avoid feeding lists that contain interior empty values if you need a real array.
test{jsonarray:}
Expected[]
Actual[]
An empty (or all-empty) input produces an empty JSON array, which is convenient as a safe default in JavaScript or JSON contexts.
virtual<script>const tags = {jsonarray:news-events-press};</script>
Expected
">Actual
A common use is turning a delimited field value into a JSON array a script can read. In real templates the first parameter is usually a field-getter, for example a multi-value category field; here a literal list keeps the example deterministic.