YAML ⇄ JSON API
Convert YAML to JSON or JSON to YAML in one endpoint, with multi-document support and syntax errors reported with line and column.
Send yaml to get JSON, or json to get YAML; the direction is picked automatically and can be forced with to=json or to=yaml. YAML can also be posted as a raw text body (for example text/yaml) with to=json.
YAML to JSON returns valid, the parsed value in json and the number of documents; files with several documents separated by --- return an array. Invalid YAML returns valid: false with an error message, line and column instead of an HTTP error.
JSON to YAML accepts a JSON value or JSON string in json and returns the YAML text in yaml; indent sets the indentation (1 to 8, default 2). The request body is limited to 5 MB.
Parameters
| Name | In | Type | Required | Default | |
|---|---|---|---|---|---|
yaml | body | string | no | YAML text to convert to JSON. Can also be sent as a raw text body with to=json. | |
json | body | object | no | JSON value or JSON string to convert to YAML. | |
to | body|query | string | no | Target format: json or yaml. Default: json when yaml is sent, otherwise yaml. | |
indent | body|query | number | no | 2 | YAML indentation (1-8) when converting to YAML. |
Example
curl -X POST "https://apibox.io.vn/v1/yaml" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"yaml":"name: APIBox\nversion: 1\nfeatures:\n - html\n - json\nenabled: true"}'const res = await fetch('https://apibox.io.vn/v1/yaml', {
method: 'POST',
headers: {
Authorization: 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"yaml": "name: APIBox\nversion: 1\nfeatures:\n - html\n - json\nenabled: true"
}),
});
const data = await res.json();
console.log(data);import requests
res = requests.post(
"https://apibox.io.vn/v1/yaml",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={"yaml": "name: APIBox\nversion: 1\nfeatures:\n - html\n - json\nenabled: true"},
timeout=30,
)
print(res.json())<?php
$ch = curl_init('https://apibox.io.vn/v1/yaml');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => ['Authorization: Bearer YOUR_API_KEY', 'Content-Type: application/json'],
CURLOPT_POSTFIELDS => '{"yaml":"name: APIBox\\nversion: 1\\nfeatures:\\n - html\\n - json\\nenabled: true"}',
]);
$response = curl_exec($ch);
print_r(json_decode($response, true));Response
{
"valid": true,
"json": {
"name": "APIBox",
"version": 1,
"features": [
"html",
"json"
],
"enabled": true
},
"documents": 1
}
Use cases
- Convert Kubernetes, Docker Compose or GitHub Actions YAML to JSON
- Generate YAML config files from JSON data
- Validate YAML syntax in CI with exact error positions
- Read OpenAPI specs written in YAML from JavaScript
FAQ
How do I choose the conversion direction?
Send yaml to get JSON, or json to get YAML. To be explicit, add to=json or to=yaml.
Are multi-document YAML files supported?
Yes. Documents separated by --- are returned as an array in json, and documents gives their count.
How are YAML syntax errors reported?
The response is valid: false with error.message, error.line and error.column.
Related APIs
JSON Validator
Check JSON syntax with exact line and column of errors, and validate data against a JSON Schema (draft 4, 7, 2019-09 or 2020-12).
{ }JSON Formatter
Pretty print JSON with 0-8 spaces or tabs, minify it, or sort object keys recursively. Syntax errors are reported with line and column.
CSVCSV to JSON
Convert CSV into an array of JSON objects using the header row, with delimiter auto-detection, number and boolean typing, and row-level errors.