Netlify Configuration TOML to JSON
Convert Netlify's netlify.toml deployment configuration to JSON. Covers build settings, redirects, headers, environment-specific contexts, and plugin configuration.
Detailed Explanation
netlify.toml is the configuration file for Netlify deployments. It defines build commands, redirect rules, custom headers, and environment-specific overrides. Understanding its JSON structure helps when working with Netlify's API or generating configs programmatically.
A typical netlify.toml:
[build]
command = "npm run build"
publish = "dist"
[build.environment]
NODE_VERSION = "20"
NPM_FLAGS = "--legacy-peer-deps"
[context.production]
command = "npm run build:prod"
[context.deploy-preview]
command = "npm run build:preview"
[[redirects]]
from = "/old-page"
to = "/new-page"
status = 301
[[redirects]]
from = "/api/*"
to = "https://api.example.com/:splat"
status = 200
force = true
[[headers]]
for = "/*"
[headers.values]
X-Frame-Options = "DENY"
X-Content-Type-Options = "nosniff"
Cache-Control = "public, max-age=3600"
[[plugins]]
package = "@netlify/plugin-lighthouse"
[plugins.inputs]
audits = ["performance", "accessibility"]
Converts to JSON:
{
"build": {
"command": "npm run build",
"publish": "dist",
"environment": {
"NODE_VERSION": "20",
"NPM_FLAGS": "--legacy-peer-deps"
}
},
"context": {
"production": {
"command": "npm run build:prod"
},
"deploy-preview": {
"command": "npm run build:preview"
}
},
"redirects": [
{"from": "/old-page", "to": "/new-page", "status": 301},
{"from": "/api/*", "to": "https://api.example.com/:splat", "status": 200, "force": true}
],
"headers": [
{
"for": "/*",
"values": {
"X-Frame-Options": "DENY",
"X-Content-Type-Options": "nosniff",
"Cache-Control": "public, max-age=3600"
}
}
],
"plugins": [
{
"package": "@netlify/plugin-lighthouse",
"inputs": {
"audits": ["performance", "accessibility"]
}
}
]
}
Notable conversion details:
[[redirects]],[[headers]], and[[plugins]]all use array of tables, producing JSON arrays.- Context overrides use standard nested tables (
[context.production]), creating a JSON object with environment names as keys. - Sub-tables within arrays like
[headers.values]and[plugins.inputs]add nested objects to the current array element. - Build environment is a nested table under the
buildsection.
Netlify also supports JSON configuration directly, so understanding the mapping enables seamless format switching.
Use Case
Generating Netlify deployment configurations dynamically from a CI/CD pipeline, where redirect rules and headers are computed from a database or API and need to be written as either TOML or JSON.