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.
The examples below are reference-only. json performs real IO (filesystem, process, network, or environment access) that the in-browser playground's sandbox cannot provide, so it is not registered there and its examples have no Run button. Pure-compute modules such as strings and json run their examples live in the page.
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 "encoding/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 "encoding/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, " "));