The JSON Module¶
Peel makes interacting with JSON easy through the JSON global object.
Parsing JSON¶
The JSON.parse function takes a string and converts it into a Peel object or value.
let raw_json = "{\"name\": \"Peel Coder\", \"level\": 99}";
let user = JSON.parse(raw_json);
fmt.println("Username:", user.name);
Stringifying JSON¶
The JSON.stringify function takes any Peel value and converts it into a JSON string.
let data = {
theme: "dark",
auto_save: true
};
let payload = JSON.stringify(data);
fmt.println(payload); // {"theme":"dark","auto_save":true}
Handling Types¶
- Peel
Listbecomes a JSON Array. - Peel
Objectbecomes a JSON Object. intandfloatbecome JSON Numbers.boolandstringare mapped directly.Voidbecomesnull.