Tinderbox v9 Icon

String.jsonEncode("string")


Operator Type: 

Operator Scope of Action: 

Operator Purpose: 

Operator First Added: 

Operator Altered: 

 Function   [other Function type actions]

 Item   [operators of similar scope]

 Formatting   [other Formatting operators]

 9.1.0

 


String.jsonEncode()

From v9.1.0, this 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.)