JSON Module
Import using: use json
json.parse(jsonString)
Parses a JSON formatted string and maps it into corresponding arrays or maps. Throws on format violations.
use json
let doc = '{ "user": "Riad", "scores": [98, 92, 100] }'
let obj = json.parse(doc)
print(obj["user"]) # Riad
print(str(obj["scores"])) # [98, 92, 100]
# Invalid JSON throws
let invalid = json.parse("{bad}")
json.stringify(data, replacer, space)
Serializes values into standard JSON format. Space specifies indentation (integer for spaces, or boolean).
use json
let data = {"name": "Djazair", "version": 1.0}
let str = json.stringify(data, Null, True)
print(str)
json.pretty(data)
Serializes data with neat layout formatting (alias for stringify with indentation).
use json
let data = {"nested": {"a": 1, "b": [2, 3]}}
print(json.pretty(data))
json.isValid(jsonString)
Checks structure validity without throwing. Returns boolean.
use json
print(str(json.isValid('{"ok": 1}'))) # True
print(str(json.isValid("{bad}"))) # False
Round-Trip Example
use json
let doc = '{"user": "Riad", "verified": true, "scores": [98, 92, 100]}'
let obj = json.parse(doc)
let back = json.stringify(obj, Null, True)
print(back)
print(str(json.isValid(doc))) # True