{jsonstring:string}

Description

Wraps a value as a JSON string literal: it adds the surrounding double quotes and escapes the characters JSON requires - the double quote, the backslash, and control characters such as newline and tab. Forward slashes and accented or non-ASCII letters are left as they are (the JSON standard allows this and parsers accept it); input that is not already UTF-8 is converted to UTF-8 first. Leading and trailing whitespace is trimmed before encoding. The typical use is inserting a text field value into a hand-written JSON object or a JavaScript function call so its quotes and newlines cannot break the surrounding syntax. The output already includes the quotes, so do not add your own around it.

Parameters

string optional

The text to encode. Usually a field-getter such as string or {headline........}, but any expression or literal works. Leading and trailing whitespace is trimmed, then the result is returned wrapped in double quotes with JSON escaping applied. If omitted, the output is an empty JSON string: a pair of double quotes.

Examples

test[{jsonstring:Hello}]
Expected["Hello"]
Actual["Hello"]
A plain word is wrapped in double quotes. The surrounding square brackets are literal text in the example, only the {jsonstring:...} part is the command output.
test[{jsonstring:She said "stop" loudly}]
Expected["She said \"stop\" loudly"]
Actual["She said \"stop\" loudly"]
Double quotes inside the value are escaped with a backslash so they do not close the JSON string early. This is the main reason to use the command instead of typing your own quotes.
test[{jsonstring:path\to\file}]
Expected["path\\to\\file"]
Actual["path\\to\\file"]
A backslash is doubled, because a single backslash starts an escape sequence in JSON.
test[{jsonstring:C#:\Temp}]
Expected["C:\\Temp"]
Actual["C:\\Temp"]
A colon is the parameter separator in {cmd:...}, so a literal colon in the value must be written as a hash followed by a colon. Without it, everything after the first colon is dropped. Here the input C followed by hash-colon then backslash Temp encodes to the path C colon backslash-backslash Temp.
test[{jsonstring:reports/2024/q1}]
Expected["reports/2024/q1"]
Actual["reports/2024/q1"]
Forward slashes are NOT escaped. The JSON standard allows a bare slash and every parser accepts it, so a URL or path stays readable. PHP json_encode would escape them by default; this command turns that off.
test[{jsonstring: Praha }]
Expected["Praha"]
Actual["Praha"]
Leading and trailing whitespace is removed before encoding (the command inherits parameter trimming). Inner spaces are kept.
test[{jsonstring:}]
Expected[""]
Actual[""]
With no value the output is an empty JSON string: just a pair of double quotes. Useful as a safe default in generated JSON.
test{"label":{jsonstring:Tom's "news" pick}}
Expected{"label":"Tom's \"news\" pick"}
Actual{"label":"Tom's \"news\" pick"}
The everyday pattern: hand-write the JSON object and let the command supply the quoted, escaped value. In a real view the subject would be a field-getter, for example {"label":{jsonstring:_#HEADLINE}}. Note the value carries its own quotes, so none are typed around it.
virtual[{jsonstring:_#FULLTEXT}]
Expected["First line\nSecond line"] (when the field holds two lines)
Actual["_#FULLTEXT"]
Control characters are escaped too: a real newline in the input becomes the two-character sequence backslash-n, a tab becomes backslash-t. This keeps a multi-line text field on one valid JSON line. Marked illustrative because the output depends on the field content.
virtual<button onclick="addToCart(_#ITEM_ID,{jsonstring:_#HEADLINE})">Add</button>
Expected (the headline arrives safely quoted and escaped)
Add (the headline arrives safely quoted and escaped)">Actual
A live pattern from production: jsonstring supplies the quoted, escaped string argument so an apostrophe or quote in the headline does not break the onclick JavaScript. Wrap the whole attribute value in {safe:...} when the value may also contain HTML-special characters. Marked illustrative because the output is HTML the browser renders.