@eryx/encoding/json Module

JSON

JSON encoding and decoding.

Serialises Luau values to JSON text and parses JSON text back into Luau values. Follows RFC 8259 with sensible Luau-specific defaults.

local json = require("@eryx/encoding/json")

local str = json.encode({ name = "Alice", scores = {100, 97} })
print(str)  -- '{"name":"Alice","scores":[100,97]}'

local tbl = json.decode('{"x":1,"y":2}')
print(tbl.x, tbl.y)  -- 1  2

Mapping rules:

Luau JSON
string string
number number
boolean true / false
nil null
Numeric table array []
Mixed table object {}

Summary

Functions

json.encode(value: any, options: EncodeOptions?)string
json.decode(str: string)any

API Reference

Functions

json.encode

Encodes a Luau value as a JSON string.

Tables with sequential integer keys (1 .. n) are encoded as JSON arrays. Tables with string keys are encoded as JSON objects. Nested structures are fully supported.

Set options.pretty to true for indented, human-readable output. Set options.sortKeys to true to emit object keys in alphabetical order.

json.encode(value: any, options: EncodeOptions?)string

Parameters

value: any

-- The value to encode.

options: EncodeOptions?

Optional formatting options.

Returns

string

The JSON string.

json.decode

Decodes a JSON string into a Luau value.

JSON Luau
string string
number number
true / false boolean
null nil
array [] Numeric table
object {} { [string]: any }

Note: JSON null values inside arrays will create holes in the resulting Luau table. Standalone null at the top level returns nil.

json.decode(str: string)any

Parameters

str: string

A valid JSON string.

Returns

any

The decoded Luau value.

Types

EncodeOptions

pretty: boolean?

Insert newlines and indentation for readability.

indent: string?

Indentation string used when pretty is enabled (default " " - two spaces).

sortKeys: boolean?

Sort object keys alphabetically.