Parsing and writing YAML in Deno with @std/yaml

YAML is a common format for configuration files. Deno’s standard library ships a YAML module, @std/yaml, with two main functions: parse turns YAML text into JavaScript values, and stringify turns values back into YAML. The examples were checked against @std/yaml 1.3.0 on Deno 2.
Importing the module
Section titled “Importing the module”Either import it directly with a jsr: specifier:
import { parse, stringify } from "jsr:@std/yaml@^1";or add it to your project’s deno.json and import it by name:
deno add jsr:@std/yamlimport { parse, stringify } from "@std/yaml";Parsing YAML
Section titled “Parsing YAML”parse returns unknown, because the module can’t know the shape of your file. Cast (or, better, validate) the result into a type before using it:
import { parse } from "jsr:@std/yaml@^1";
interface Person { name: string; age: number; hobbies: string[];}
const yamlText = `name: John Doeage: 30hobbies: - programming - reading`;
const person = parse(yamlText) as Person;console.log(person);// { name: "John Doe", age: 30, hobbies: [ "programming", "reading" ] }Without the cast, person.hobbies is a type error under deno check. A cast doesn’t check anything at runtime, so for files you don’t control, validate the shape with a schema library such as Zod before trusting it.
For a file on disk, read it first:
const config = parse(await Deno.readTextFile("config.yaml"));This needs --allow-read. If the YAML is invalid, parse throws an error with the line and column of the problem, so wrap it in try/catch when the input comes from users.
A file with several documents separated by --- needs parseAll, which returns an array:
import { parseAll } from "jsr:@std/yaml@^1";
console.log(parseAll("a: 1\n---\na: 2\n")); // [ { a: 1 }, { a: 2 } ]Editing and writing YAML
Section titled “Editing and writing YAML”Once parsed, the data is a plain object, so you change it like any other object and pass it to stringify:
import { parse, stringify } from "jsr:@std/yaml@^1";
interface Person { name: string; age: number; hobbies: string[];}
const person = parse(`name: John Doeage: 30hobbies: - programming - reading`) as Person;
person.hobbies.push("traveling");person.age += 1;
console.log(stringify(person));Output:
name: John Doeage: 31hobbies: - programming - reading - travelingTo write it back to disk, use await Deno.writeTextFile("person.yaml", stringify(person)) with --allow-write. Comments and the original formatting are not preserved: stringify writes the data, not the source text you read.
Gotchas
Section titled “Gotchas”- Unquoted numbers lose formatting.
version: 1.10parses as the number1.1. Quote version strings in your YAML (version: "1.10"). yes/nostay strings.@std/yamluses the YAML 1.2 core schema, soenabled: yesparses as the string"yes", nottrue. When writing,stringifyquotes such strings (note: 'yes') so YAML 1.1 parsers don’t read them as booleans.- No round-tripping of comments. If you need to edit a config file and keep its comments,
@std/yamlisn’t the right tool; you’d need a library that works on the document AST, such as theyamlpackage on npm.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)