json - reading and writing JSON.
Unmarshal turns the text into the values of the language, an object into a map and an array into a list, and Marshal writes them back:
json = import("encoding/json")# A raw string, or the braces would be read as interpolation and the
# quotes would need escaping.
v = json.Unmarshal(`{"name": "tau", "tags": [1, 2]}`)
println(v["name"], v["tags"][0])
println(json.MarshalIndent(v, " "))What it reads is JSON and nothing more: no trailing commas, no comments, no single quotes. A text it will not take comes back as an error saying at which line and column it gave up. An escape of the form \uXXXX is kept as it was written rather than turned into the character it names.
¶Parser = fn(src)source
--- Parser constructor ---
¶Unmarshal = fn(s)source
Public API
¶Marshal = fn(v)source
Marshal returns the JSON form of a value: null, booleans, numbers, strings, lists, maps and objects, which are written as objects the way a struct is in Go.
¶Valid = fn(s)source
Valid reports whether s holds one well formed JSON value and nothing else.
¶MarshalIndent = fn(v, indent)source
MarshalIndent is Marshal with the output spread over several lines, indent being what one level is written with. With no indent a tab is used.
¶Indent = fn(src, indent)source
Indent rewrites JSON with one value per line. It walks the text rather than the value, so what is inside strings is left alone. The braces are written as raw strings: in an ordinary one a brace opens an interpolation.