Skip to content

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.

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:

Terminal window
deno add jsr:@std/yaml
import { parse, stringify } from "@std/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 Doe
age: 30
hobbies:
- 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 } ]

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 Doe
age: 30
hobbies:
- programming
- reading
`) as Person;
person.hobbies.push("traveling");
person.age += 1;
console.log(stringify(person));

Output:

name: John Doe
age: 31
hobbies:
- programming
- reading
- traveling

To 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.

  • Unquoted numbers lose formatting. version: 1.10 parses as the number 1.1. Quote version strings in your YAML (version: "1.10").
  • yes/no stay strings. @std/yaml uses the YAML 1.2 core schema, so enabled: yes parses as the string "yes", not true. When writing, stringify quotes 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/yaml isn’t the right tool; you’d need a library that works on the document AST, such as the yaml package on npm.

© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)