Skip to content

JSON

The json() builder renders design tokens as JSON output. Choose flat or nested structure and optionally include DTCG metadata ($type, $description).

import { json } from 'dispersa'
OptionTypeDefaultDescription
namestringUnique output identifier
filestring or functionOutput path (supports {*context*} interpolation for modifiers)
preset’bundle’ | ‘standalone''standalone’Output preset
structure’flat’ | ‘nested''nested’Token structure
includeMetadatabooleanInclude $type, $description
minifybooleanMinify output
transformsTransform[]Per-output transforms
filtersFilter[]Per-output filters
import { Dispersa, json } from 'dispersa'
import { nameKebabCase, colorToHex, dimensionToPx } from 'dispersa/transforms'
await dispersa.build({
outputs: [
json({
name: 'flat',
file: 'tokens.json',
structure: 'flat',
transforms: [nameKebabCase(), colorToHex(), dimensionToPx()],
}),
],
})

Output:

{
"color-brand-primary": "#0066cc",
"spacing-medium": "16px"
}
json({
name: 'nested',
file: 'tokens.json',
structure: 'nested',
transforms: [colorToHex(), dimensionToPx()],
})

Output:

{
"color": {
"brand": {
"primary": "#0066cc"
}
},
"spacing": {
"medium": "16px"
}
}

When includeMetadata is true, the output includes DTCG $type and $description fields:

json({
name: 'with-meta',
file: 'tokens.json',
structure: 'nested',
includeMetadata: true,
transforms: [colorToHex()],
})

Emit one JSON file per modifier (e.g., per theme):

json({
name: 'themes',
file: 'tokens-{theme}.json',
preset: 'standalone',
structure: 'flat',
transforms: [nameKebabCase(), colorToHex()],
})