---
seo_title: API JSON in TOML | Converti file JSON in TOML | EnConvert
meta_desc: Converti JSON in TOML con POST /v1/convert/json-to-toml. Carica un file .json, ricevi TOML diretto o via URL prefirmato. I valori null causano errore 400.
keywords: json in toml api, convertire json in toml, api conversione json toml, json to toml rest api, convertire file json in toml, json in toml python, json in toml curl, convertire configurazione json in toml api
---

# API JSON in TOML

L'API JSON in TOML converte un file JSON in formato TOML con una singola richiesta a `POST /v1/convert/json-to-toml`. Carica un file `.json` tramite multipart form data e ricevi il TOML convertito direttamente, oppure imposta `direct_download=false` per ottenere i metadati con un URL di download prefirmato. Gli array JSON vengono racchiusi sotto una chiave `items` e i valori scalari sotto una chiave `value`, poiché TOML richiede una tabella di primo livello.

---

## Endpoint

```
POST /v1/convert/json-to-toml
```

**Content-Type:** `multipart/form-data`

**Input accettato:** file `.json` (codificati in UTF-8)

**Formato di output:** `.toml` (`application/toml`)

---

## Autenticazione

Richiede una chiave API privata oppure un token JWT ottenuto da una chiave pubblica.

```
X-API-Key: sk_your_private_key
```

Oppure:

```
Authorization: Bearer <jwt_token>
```

---

## Parametri della richiesta

| Parametro | Tipo | Obbligatorio | Predefinito | Descrizione |
|-----------|------|----------|---------|-------------|
| `file` | file | Sì | -- | Il file `.json` da convertire. Deve essere codificato in UTF-8. Non deve contenere valori `null`. |
| `output_filename` | `string` | No | Nome file di input | Nome file di output personalizzato. L'estensione `.toml` viene aggiunta automaticamente. |
| `direct_download` | `boolean` | No | `true` | Con `true`, restituisce i byte TOML grezzi. Con `false`, restituisce i metadati con un URL di download prefirmato. |

---

## Regole di conversione

- **Oggetto JSON** passa direttamente:
  ```json
  {"database": {"host": "localhost", "port": 5432}}
  ```
  Diventa:
  ```toml
  [database]
  host = "localhost"
  port = 5432
  ```

- **Array JSON** viene racchiuso sotto una chiave `items` (TOML richiede una tabella di primo livello):
  ```json
  [1, 2, 3]
  ```
  Diventa:
  ```toml
  items = [1, 2, 3]
  ```

- **Valore scalare** viene racchiuso sotto una chiave `value`:
  ```json
  42
  ```
  Diventa:
  ```toml
  value = 42
  ```

<div class="alert alert-warning">
<strong>Importante:</strong> TOML non supporta i valori <code>null</code>. Se il tuo JSON contiene valori <code>null</code>, la conversione fallirà con un errore 400. Rimuovi o sostituisci i valori <code>null</code> prima di convertire.
</div>

---

## Risposta

### Download diretto (`direct_download=true`, predefinito)

```
HTTP 200 OK
Content-Type: application/toml
Content-Disposition: inline; filename="config_20260405_123456789.toml"
```

### Risposta con metadati (`direct_download=false`)

```json
{
    "presigned_url": "https://spaces.example.com/...",
    "object_key": "env/files/{project_id}/json-to-toml/config_20260405_123456789.toml",
    "filename": "config_20260405_123456789.toml",
    "file_size": 1234,
    "conversion_time_seconds": 0.03
}
```

---

## Esempi di codice

### Python

```python
import requests

with open("config.json", "rb") as f:
    response = requests.post(
        "https://api.enconvert.com/v1/convert/json-to-toml",
        headers={"X-API-Key": "sk_your_private_key"},
        files={"file": ("config.json", f, "application/json")}
    )

with open("config.toml", "wb") as out:
    out.write(response.content)
```

### Node.js

```javascript
const form = new FormData();
form.append("file", fs.createReadStream("config.json"));

const response = await fetch("https://api.enconvert.com/v1/convert/json-to-toml", {
    method: "POST",
    headers: { "X-API-Key": "sk_your_private_key" },
    body: form
});

const toml = await response.text();
```

### PHP

```php
$ch = curl_init("https://api.enconvert.com/v1/convert/json-to-toml");
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => ["X-API-Key: sk_your_private_key"],
    CURLOPT_POSTFIELDS => ["file" => new CURLFile("config.json", "application/json")]
]);
$toml = curl_exec($ch);
curl_close($ch);
```

### Go

```go
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, _ := writer.CreateFormFile("file", "config.json")
file, _ := os.Open("config.json")
io.Copy(part, file)
writer.Close()

req, _ := http.NewRequest("POST", "https://api.enconvert.com/v1/convert/json-to-toml", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
req.Header.Set("X-API-Key", "sk_your_private_key")
resp, _ := http.DefaultClient.Do(req)
```

---

## Risposte di errore

| Stato | Condizione |
|--------|-----------|
| `400 Bad Request` | Il file non è un file `.json` |
| `400 Bad Request` | Contenuto JSON non valido |
| `400 Bad Request` | Conversione da JSON a TOML non riuscita (tipicamente a causa di valori `null`) |
| `401 Unauthorized` | Chiave API / token JWT mancante o non valido |
| `402 Payment Required` | Quota mensile di ops esaurita |
| `413 Payload Too Large` | Il file supera la dimensione massima prevista dal piano |

---

## Limiti

| Limite | Valore |
|-------|-------|
| Dimensione massima del file | Dipende dal piano (Founding: 5 MB) |
| Codifica di input | Solo UTF-8 |
| Valori null | Non supportati (limitazione di TOML) |
| Conversioni mensili | Dipende dal piano |

---

## Domande frequenti

### Come convertire JSON in TOML con un'API REST?

Invia una richiesta POST `multipart/form-data` a `POST /v1/convert/json-to-toml` con il file `.json` nel campo `file`, autenticandoti con un header `X-API-Key` o un token JWT `Authorization: Bearer`. Per impostazione predefinita il corpo della risposta contiene i byte TOML grezzi.

### Perché la conversione da JSON a TOML fallisce con un errore 400?

TOML non supporta i valori `null`, quindi qualsiasi JSON contenente `null` fallisce con `400 Bad Request`. Un `400` viene restituito anche quando il file caricato non è un `.json` o il contenuto JSON non è valido.

### Posso convertire in TOML un array JSON o un valore scalare?

Sì. Poiché TOML richiede una tabella di primo livello, un array JSON viene racchiuso sotto una chiave `items` e un valore scalare sotto una chiave `value`.

### Come ottengo un URL di download invece del file TOML grezzo?

Imposta `direct_download=false` nella richiesta. L'API restituisce quindi metadati JSON che includono `presigned_url`, `object_key`, `filename`, `file_size` e `conversion_time_seconds`.

### Quale limite di dimensione file si applica alle conversioni da JSON a TOML?

La dimensione massima del file dipende dal piano (piano Founding: 5 MB). I file che superano il limite del tuo piano restituiscono `413 Payload Too Large`.
