JSON

json.escape

Escapes text so it can safely appear inside a JSON string literal.

Syntax

json.escape(text: string): string

Arguments

NameTypeDescription
textstringText value used by the operation.

Returns

NameTypeDescription
escapedstringEscaped JSON string content.

Description

Escapes text so it can safely appear inside a JSON string literal.

Call it with 1 parameter(s): text. The argument table explains which values are required and which ones only refine the behavior.

It returns escaped (string). Use the returns table to separate successful values from nil results and recoverable errors.

Example

Escape text for use inside a JSON string.

local source = "line 1" .. string.char(10) .. "line 2"
local escaped = json.escape(source)

print(escaped)