json
The json library parses JSON strings into Scriptling objects and serializes Scriptling objects back to JSON strings.
Available Functions
| Function | Description |
|---|---|
loads(string) |
Parse a JSON string into dict, list, str, int, float, bool or None; objects keep the document’s key order; raises on invalid JSON. |
dumps(object, indent=None, separators=None, sort_keys=False, ensure_ascii=True) |
Serialise to a JSON string, matching Python’s json.dumps: keys in dict insertion order, ", " / ": " separators, non-ASCII escaped. |
parse(string) |
Alias for loads(). |
stringify(object, indent=None) |
Alias for dumps(). |
Example
import json
data = json.loads('{"users": [{"name": "Alice"}, {"name": "Bob"}], "ok": true}')
print(data["users"][0]["name"], data["ok"]) # Alice True
obj = {"status": "success", "count": 42}
print(json.dumps(obj)) # {"status": "success", "count": 42}
print(json.dumps(obj, separators=(",", ":"))) # {"status":"success","count":42}
print(json.dumps(obj, indent=2)) # pretty-printed, two spaces per level
# {
# "status": "success",
# "count": 42
# }
print(json.stringify(json.parse('[1, 2.5, null]'))) # [1, 2.5, null]
try:
json.loads("{invalid json}")
except Exception as e:
print("JSON parse error:", e) # JSON parse error: JSONDecodeError: invalid character 'i' looking for beginning of object key stringDifferences from Python
loads()builds dicts in the document’s key order (a repeated key keeps its first position and its last value), soloads()followed bydumps()round-trips key order. Integers beyond 64 bits become floats, since scriptling integers are 64-bit.dumps()matches Python by default: dict keys keep the dict’s insertion order, separators are", "and": ", and non-ASCII is\uescaped.indentaccepts a number of spaces or a literal string;indent=0puts each item on its own line without indentation.sort_keys=True,separators=(item, key)andensure_ascii=Falseare honoured.dumps()writes non-finite floats as the bare tokensNaN,Infinityand-Infinity, exactly like Python.loads()cannot read those tokens back (strict JSON only), so round-trip non-finite values as strings ornull.parse()andstringify()are Scriptling aliases. There are no file-basedload()/dump()functions: read the file to a string first.
See Also
- msgpack: binary serialisation — more compact, not human-readable.
- bytes: Scriptling’s binary data type.
- toml: parse and generate TOML configuration data.
- yaml: parse and generate YAML data.
- scriptling.csv: parse and generate CSV data.
- scriptling.xml: parse and generate XML data.