Skip to content

json

let json = import("json")
Function Returns Description
parse(text: string) map | array | string | int | float | bool | none Parse a JSON string; returns a value or none on invalid input
stringify(value) string Compact JSON string
let json = import("json")
let data = json.parse("{\"name\":\"Ada\",\"age\":36,\"tags\":[\"math\",\"code\"]}")
print(data.name) # Ada
print(data.tags[0]) # math
data.age = 37
print(json.stringify(data))
# {"name":"Ada","age":37,"tags":["math","code"]}
JSON Pyle
object map
array array
string string
number without a fraction int
number with a fraction or exponent float
true / false bool
null none

Parsed objects become maps, so dot reads work (data.title). Dot writes also work (data.title = "x").

  • json.stringify emits null for none.
  • Struct instances raise TypeError: Cannot serialize type 'struct' to JSON. Convert them to a map first.
  • Invalid input to json.parse returns none rather than raising. Check the result.
let parsed = json.parse(maybe_json)
if parsed == none {
print("invalid json")
}
  • worker uses JSON to move tasks across process boundaries.
  • http returns JSON bodies as strings; parse them with json.parse.