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) # Adaprint(data.tags[0]) # math
data.age = 37print(json.stringify(data))# {"name":"Ada","age":37,"tags":["math","code"]}Mapping
Section titled “Mapping”| 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.stringifyemitsnullfornone.- Struct instances raise
TypeError: Cannot serialize type 'struct' to JSON.Convert them to a map first. - Invalid input to
json.parsereturnsnonerather than raising. Check the result.
let parsed = json.parse(maybe_json)if parsed == none { print("invalid json")}