Atom

json.encode()

Serializes an Atom value to JSON.

Signature

json.encode(value[, pretty])

Parameters

Supported scalar/collection and optional boolean pretty flag, default false.

Returns

String.

Behavior

Arrays and dictionaries recurse through their values. A char encodes as a JSON string.

Errors

Nonfinite number, cycle, excessive nesting, nontext dictionary key or unsupported value.

Example

Atom
use json
assert(json.encode({x = 1}) == '\{"x":1}')

Encode one value

JSON can hold a scalar such as an integer.

Atom
use json
print(json.encode(42))
Output
42

The output looks like a number, but encode() returned JSON text.

Encode a small object

A dictionary makes a JSON object when its keys are text.

Atom
use json
print(json.encode({name = 'Ada'}))
Output
{"name":"Ada"}

The dictionary key and its text value become JSON strings.