Reference · JSON

json.stringify

signature
json.stringify(any, any=) -> str

What it does

The value as JSON text, on one line with no spaces. With a second argument that is true, the text is indented by two spaces, one entry per line. A map becomes an object with its keys in the order they were set (in the browser too, keys that look like numbers included: { b: 1, "1": 2 } is {"b":1,"1":2} on both sides), a list an array, a decimal a string of its digits so that nothing reading it rounds it. A route that answers a map writes the same JSON.

Examples

let user = { name: "Ann", tags: ["a", "b"], n: 3, ok: true, x: null }print(json.stringify(user))print(json.stringify(user, true))print(json.stringify([1, 2]), json.stringify("hi"), json.stringify(42), json.stringify(null))
output
{"name":"Ann","tags":["a","b"],"n":3,"ok":true,"x":null}{  "name": "Ann",  "tags": [    "a",    "b"  ],  "n": 3,  "ok": true,  "x": null}[1,2] "hi" 42 null

Edge cases

print(json.stringify(2.0), json.stringify(2), json.stringify(0.1 + 0.2))print(json.stringify(1e21), json.stringify(1e20), json.stringify(1e-7), json.stringify(0.000001), json.stringify(-0.0))print(json.stringify(12.50m), json.stringify(float("nan")))print(json.stringify(err("boom")), json.stringify(err({ code: 1 })))print(json.stringify("quote \" and é"))print(json.stringify({ a: 1 }, 1), json.stringify({ a: 1 }, 0))print(json.stringify(raw("<b>")))
output
2.0 2 0.300000000000000041e+21 100000000000000000000.0 1e-7 0.000001 -0.0"12.50" null{"error":"boom"} {"error":{"code":1}}"quote \" and é"{  "a": 1} {  "a": 1}"<b>"

Errors

A function anywhere in the value gives an error value naming its path, on both sides (it was written as its text). So does a value whose JSON would pass 256 MiB or nest more than 10,000 deep. A route that answers a value holding a function is a 500, with a log line naming the path. Note that a float past 2⁵³ written by it and read by JavaScript loses digits; Quillo's own json.parse reads it back whole.

print(json.stringify({ user: { name: "Ann", greet: (x) => x } }))print(json.stringify([1, print]), json.stringify(print))print(json.parse(json.stringify({ big: 9007199254740993 })))
output
err("json.stringify: `user.greet` is a function, which cannot be written as JSON")err("json.stringify: `[1]` is a function, which cannot be written as JSON") err("json.stringify: a function cannot be written as JSON"){ big: 9007199254740993 }

Chaining

print({ a: 1 } | json.stringify | len)print([1, 2] | json.stringify | json.parse)print(crypto.sha256(json.stringify({ a: 1 })) | slice(0, 8))
output
7[1, 2]015abd7f

See also

json.parse, str, dec, err, crypto.sha256

Thread
6Useful? Like it, or add to its thread below.

Thread 0 posts

On json.stringify(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo