Synsema docsENES

JSON

Two builtins, fully round-trippable: json_decode(json_encode(x)) reconstructs x.

json.syn
-- Doc example: JSON encode/decode (pure, round-trippable).
intent: "doc example: JSON"

print(json_encode({"name": "Ada", "tags": ["x", "y"], "n": 3}))    -- run shows the JSON

test "encode then decode reconstructs the value"
    let data be {"name": "Ada", "tags": ["x", "y"], "n": 3}
    let s be json_encode(data)
    let back be json_decode(s)
    assert_eq(back["name"], "Ada")
    assert_eq(back["n"], 3)
    assert_eq(length(back["tags"]), 2)

Encode & decode

json_encode(value)      -- value → JSON text
json_decode(text)       -- JSON text → value (object→map, array→list, …)

What encode does with special values

This is the idiomatic way to store structured data in a text/Redis value:

redis_set("cfg", json_encode({"theme": "dark", "n": 3}))
let cfg be json_decode(redis_get("cfg"))

json_decode errors clearly on invalid JSON. Note that responses from the HTTP client already expose a parsed json of r (see HTTP client).