json
JSON encode/decode.
Naming convention: import the module under its bare name (
import "json"), reach members with a backslash, and call methods incamelCase:json\someMethod.
Methods
parse
Decode a JSON string into a value (map, list, string, number, or boolean).
Signature: json\parse(s) → any1 · source
| Parameter | Type | Optional | Description |
|---|---|---|---|
s |
string |
Returns: any
Example:
import "std";
import "json";
final v = json\parse("\{\"name\": \"api\", \"port\": 8080\}");
std\print(v["name"]);
std\print(v["port"]);
// -> api
// -> 8080
stringify
Encode a value as a JSON string. With no indent (or "") the output is compact; pass an indent string (e.g. " " or "\t") for pretty, multi-line output.
Signature: json\stringify(value, [indent]) → string2 · source
| Parameter | Type | Optional | Description |
|---|---|---|---|
value |
any |
||
indent |
string |
yes |
Returns: string
Example:
import "std";
import "json";
final config = { "target": "build", "parallel": true };
std\print(json\stringify(config));
// -> {"parallel":true,"target":"build"}
// Pretty-printed with two-space indent:
std\print(json\stringify(config, " "));