---
slug: 36-json
title: JSON
description: Serializá y parseá JSON en Synsema con json_encode / json_decode — round-trippable, con manejo seguro de secrets, bytes y decimal.
example_ids: [json]
---

# JSON

Dos builtins, totalmente round-trippables: `json_decode(json_encode(x))` reconstruye `x`.

```synsema
-- 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)
```

## Codificar y decodificar

```synsema
json_encode(value)      -- value → texto JSON
json_for_script(value)  -- el mismo JSON, `<` `>` `&` escapados \u00XX — SEGURO dentro de un <script> inline
json_decode(text)       -- texto JSON → value (object→map, array→list, …)
```

## Qué hace encode con valores especiales

- `secret` → `"[redacted]"` (seguro — nunca filtra el texto plano).
- `bytes` → un string base64.
- `decimal` (`1.50d`) → un número JSON exacto.
- `nothing` → `null`.

Es la forma idiomática de guardar data estructurada en un valor de texto/Redis:

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

`json_decode` da un error claro ante JSON inválido. Ojo: las respuestas del cliente HTTP ya exponen un `json of r` parseado (ver **[Cliente HTTP](/es/0.6.x/32-http-client)**).
