Skip to content

std.json - ZZ

JSON parse/stringify/get/as_* with an opaque json handle. Buffers (bytes) serialize as int arrays. json.parse is fallible (.err on invalid JSON) and never panics.

import std.json

std.json parses JSON strings into an opaque json handle, extracts typed fields, and serializes values back to JSON. Extraction functions (as_str, as_int, …) are total but panic on type mismatch — guard with is_* or use ?-able get first.

json is an opaque handle — you cannot inspect it directly. You navigate via get/deep_get (object) or indexing conventions, then extract via as_str/as_int/as_float/as_bool. Type checks (is_null, is_bool, is_number, is_string, is_array, is_object, is_empty) let you branch safely before extraction.

import std.json
func main() -> Result<int, str> {
data := json.parse(`{"name": "Alice", "age": 30, "tags": ["a","b"]}`)?
println(json.as_str(json.get(data, "name")?)) // Alice
println(json.as_int(json.get(data, "age")?)) // 30
person := {"name": "Bob", "age": 25}
println(json.stringify(person)?) // {"name":"Bob","age":25}
return .ok(0)
}
// Safe navigation with type checks
func safe_nav() -> Result<int, str> {
data := json.parse(`{"name": "Alice"}`)?
if json.is_object(data) && json.has(data, "name") {
println(json.as_str(json.get(data, "name")?))
}
return .ok(0)
}
  • parse returns .err(message) on invalid JSON — never panics.
  • as_* panic on type mismatch — prefer is_* guards or get + ?.
  • get/deep_get return .err on missing key — ?-able.
  • parse_or/parse_or_null never fail (tolerant).
  • json is opaque — inspect only via as_*, get, keys, is_*.
  • bytes serialize to JSON as int arrays.
  • json.stringify is generic over T — dicts, arrays, scalars all work.
  • as_* panics on mismatch — always check with is_* first in production code.
import std.json
func main() -> Result<int, str> {
// Tolerant parse
user_input := `{"address": {"city": "NYC"}}`
data := json.parse_or(user_input, json.null())
// Deep path with default
city := json.path_get_or(data, "address.city", json.null())
if json.is_string(city) {
println(json.as_str(city))
}
// Type-safe extraction
if json.is_object(data) && json.has(data, "email") {
_ = json.as_str(json.get(data, "email")?)
}
return .ok(0)
}

headers optional, defaults {}. body may be str or bytes.

Call Notes
get(url, headers={}) GET
post/put(url, body, headers={}) body str/bytes
delete(url, headers={}) DELETE
fetch(url, method="GET", headers={}, body="", timeout_ms=30000) any verb, timeout
post_json(url, body: T, headers={}) serializes any value, sets Content-Type
fetch_insecure(...) skips TLS verify — self-signed fixtures only
match http.get("https://api.example.com/users") {
.ok(res) => println("users: {res.text()}"),
.err(e) => println("failed: {e}"),
}
func main() -> Result<int, str> {
res := http.get("https://api.example.com/users")?
body := res.json()?
println(json.stringify(body) ?? "{}")
return .ok(0)
}

listen blocks with keep-alive + graceful SIGINT/SIGTERM drain. listen_cfg adds read_ms, max_reqs_conn, max_body_bytes, shutdown_ms.

28 entries. Signatures come from the compiler’s own FuncSig table, so they match the runtime exactly; descriptions come from the module guide.

Function Signature Description
std.json.array_push array_push<T>(j: json, val: T) -> json Push to array
std.json.as_bool as_bool(j: json) -> bool Extract boolean
std.json.as_float as_float(j: json) -> float Extract float
std.json.as_int as_int(j: json) -> int Extract integer
std.json.as_str as_str(j: json) -> str Extract string (panics on mismatch)
std.json.deep_get deep_get(j: json, path: str) -> Result<json, str> Dot-path lookup (“a.b.c”)
std.json.get get(j: json, key: str) -> Result<json, str> Get object field (?-able)
std.json.has has(j: json, key: str) -> bool Key exists
std.json.is_array is_array(j: json) -> bool Is array
std.json.is_bool is_bool(j: json) -> bool Is bool
std.json.is_empty is_empty(j: json) -> bool Is empty
std.json.is_null is_null(j: json) -> bool Is null
std.json.is_number is_number(j: json) -> bool Is number
std.json.is_object is_object(j: json) -> bool Is object
std.json.is_string is_string(j: json) -> bool Is string
std.json.keys keys(j: json) -> [str] Object keys
std.json.len len(j: json) -> int Array/object length
std.json.merge merge(a: json, b: json) -> json Deep merge
std.json.null null() -> json JSON null
std.json.parse parse(s: str) -> Result<json, str> Parse JSON string (.err on invalid)
std.json.parse_or parse_or(s: str, default: json) -> json Parse with default on failure
std.json.parse_or_null parse_or_null(s: str) -> json Parse, null on failure
std.json.path_exists path_exists(j: json, path: str) -> bool Path exists
std.json.path_get_or path_get_or(j: json, path: str, default: json) -> json Path with default
std.json.pretty pretty(j: json) -> str Pretty-print JSON
std.json.stringify stringify<T>(v: T) -> Result<str, str> Serialize any value to JSON
std.json.type type(j: json) -> str Type name string (“null”,“bool”,“number”,“string”,“array”,“object”)
std.json.validate validate(s: str) -> bool Validate without parsing
  • std.map — Pure dictionary helpers over plain maps: lookup, merge and keys.
  • std.set — Arrays with uniqueness: membership, union, intersection and difference.
  • std.dec — Exact decimal arithmetic carried as strings, so money never becomes a float.
  • std.csv — An RFC 4180 subset: read rows, write rows, handle quotes and commas.
  • std.property — Seeded generators and shrinking for property-based tests.

Full index: API reference · Standard library