Tinderbox v10 Icon

String.jsonEncode()


Operator Type: 

Operator Scope of Action: 

Operator Purpose: 

Data Type Returned: 

Operator First Added: 

Operator in Current Baseline: 

Operator Last Altered: 

 Function  [other Function type actions]

 Item  [operators of similar scope]

 Formatting  [other Formatting operators]

 String [about String data type]

 v9.1.0

 Baseline

 As at baseline


Syntax note: Operators without any defined mandatory arguments may omit their empty closing parentheses


String.jsonEncode()

String.jsonEncode

The .jsonEncode() operator returns a JSON-encoded UTF-8 version of the chained source String's value. Forbidden characters such as the apostrophe (straight single quote), straight double quote, solidus (forward slash) and backslash characters are all escaped by a preceding backslash character.

The general expectation is String will be a quoted literal string or a String-type attribute or variable, e.g. $Text:

var:string vEncoded = $Text.jsonEncode() 

^value("Apostrophe's are often wrongly used".jsonEncode())^ 

This operator was formerly know as .json, and is effectively a replacement for that operator. The naming also better reflects the non dot-operator jsonEncode().

Using the latter, the same outcome can be encoded as:

var:string vEncoded = jsonEncode($Text) 

^value(jsonEncode("Apostrophe's are often wrongly used"))^ 

For attributes, variables and very long literal string, the chained dot-operator seems more helpful when coding, whereas the older form may be more intuitive with short literal strings (as shown in the last example above.)


See also—notes linking to here: