A Design Tokens Workflow (part 3)
Beyond JSON: Exploring File Formats for Design Tokens
- Getting Started With Style Dictionary
- Outputting to Different Formats with Style Dictionary
- Beyond JSON: Exploring File Formats for Design Tokens – You are here
- Converting Tokens with Style Dictionary
- Organising Outputs with Style Dictionary
- Layers, referencing tokens in Style Dictionary
On This Page:
Before we go further into the series I thought we should take a look at the file formats you can use to generate your Design Tokens into the file outputs you need.
The code for this part of the series available to look at on Github.
While .json
is the most common and natively supported format in Style Dictionary, it’s not the only option. When working with design tokens, choosing the right file format is crucial for readability, maintainability, and collaboration.
While .json
is a great starting point for defining design tokens, it’s not always the most human-readable or flexible option, especially for larger or more complex token files. Custom parsers bridge this gap by enabling Style Dictionary to interpret other formats like .yaml
, .hjson
, .json5
, and the Design Tokens Working Group formats .tokens
and .tokens.json
, giving you the freedom to choose what format best suits your team’s needs.
Let's Look At File Formats
.json
JSON is natively supported by Style Dictionary, making it the easiest format to start with. Its rigid structure ensures compatibility across platforms and tools, but the verbose syntax of braces, quotes, and commas could be challenging for large or deeply nested token sets.
.yaml
YAML is a human-readable, indentation-based syntax that reduces visual clutter, making it easier to manage for humans. It can especially useful for large, nested token sets and teams with non-developers, such as designers, working with token files.
.json5
JSON5 (JSON for humans) enhances JSON by allowing comments, trailing commas, and unquoted keys, making it a more forgiving and developer-friendly format. It’s a great option if you want JSON compatibility but also need to include notes or reduce syntax errors.
.hjson
HJSON simplifies JSON by removing the need for strict punctuation, making it more human-readable. It could be a good middle ground between YAML’s simplicity and JSON’s structure.
.tokens
and .tokens.json
The .tokens
format follows the W3C Design Tokens Community Group specification, making it potentially future proof for new and existing tools and platforms. It retains the JSON structure but enforces standardised keys and metadata.
Consuming Different File Formats
Let's look how Style Dictionary can consume these formats and what, if any, extra tools we need to install to compile your Design Tokens to your desired output files.
Natively Supported Formats
Style Dictionary natively supports .json
and .tokens
'out of the box'. As previously shown in earlier articles to have your Design Tokens generate an output format (CSS Custom Properties for example) you need to define the source and platform details:
JSON
W3C .tokens
Custom Parser Formats
To use a file format that is not supported natively you would need to install the relevant library and add a custom parser to the configuration file.
YAML
To install the YAML library
We would then create a custom parser for it
We can then use .yaml
as the source in the configuration
JSON5
To install the JSON5 library.
We can then create a custom parser for it.
Again, we can then setup our configuration file to consume .json5
Design Token files
HJSON
To install the HJSON library.
Again, we need to create a custom parser
Then, we can set up the configuration file to use .hjson
files
Bringing it all together
Not that I think you would or should decide to use multiple file formats to be consumed by Style Dictionary I think, for this article and example I'll tie all the options into a single config file.
If we have installed all the packages above we can then include them in our config file along with the custom parsers and also include these file formats as our possible sources:
Let's create a simple set of Design Tokens for each file format:
color.json
border.tokens
typography.yaml
spacing.json5
animation.hjson
When running the build.js
file we will get a new CSS file containing all of these tokens as CSS custom properties:
With the configuration and example tokens above, we’ve successfully demonstrated how to use different file formats to define design tokens and process them into a single output format.
This setup showcases the flexibility of Style Dictionary, enabling teams to choose the format that best suits their workflows while maintaining consistency in the output.
In the next part of this series, we’ll return to using .json
as our design token format and dive into one of Style Dictionary’s most powerful features: automatic value transformations.
Style Dictionary allows you to define tokens in a single format, such as pixels (px), and then automatically convert them to other units based on your project’s needs. For example, you can set up tokens in px for mobile apps and have them automatically converted to rem for web development, ensuring consistent and accessible scaling.
We’ll also explore how these transformations can handle other use cases, like generating fallback colours for older browsers or converting colour formats from HEX to RGB. This flexibility reduces manual work and ensures your tokens are ready for use across multiple platforms. Stay tuned to see how these transformations can simplify and enhance your design token workflow!