Parsing and Manipulating YAML with Deno's YAML Module

Introduction
Section titled “Introduction”YAML (YAML Ain’t Markup Language) is a human-friendly data serialization format often used for configuration files and data exchange between languages with different data structures. Deno, a secure runtime for JavaScript and TypeScript, offers support for parsing and manipulating YAML through its YAML module. In this article, we will explore how to utilize this module to work with YAML data in a Deno application.
Installing and Importing the Deno YAML Module
Section titled “Installing and Importing the Deno YAML Module”Before we can use the YAML module, we first need to import the module into our project. Deno’s standard library provides the YAML module, so you can import it directly from the official repository:
import { parse, stringify } from "https://deno.land/std@0.115.0/yaml/mod.ts";Here, we import two key functions: parse and stringify. The parse function is used to convert YAML data into JavaScript objects, while the stringify function converts JavaScript objects back into YAML.
Parsing YAML Data
Section titled “Parsing YAML Data”To parse a YAML string into a JavaScript object, use the parse function:
const yamlData = `name: John Doeage: 30hobbies: - programming - reading`;
const jsObject = parse(yamlData);console.log(jsObject);This code snippet would output the following JavaScript object:
{ name: "John Doe", age: 30, hobbies: ["programming", "reading"]}Creating YAML Data
Section titled “Creating YAML Data”To convert a JavaScript object back into YAML, use the stringify function:
const jsObject = { name: "Jane Doe", age: 28, hobbies: ["painting", "hiking"],};
const yamlData = stringify(jsObject);console.log(yamlData);This code snippet would output the following YAML data:
name: Jane Doeage: 28hobbies: - painting - hikingManipulating YAML Data
Section titled “Manipulating YAML Data”Now that we know how to parse and create YAML data, let’s take a look at how to manipulate the data. Since YAML is easily converted to JavaScript objects, you can manipulate the data just like any other JavaScript object.
For example, let’s add a new hobby to the hobbies array in the jsObject:
jsObject.hobbies.push("traveling");console.log(jsObject);The modified JavaScript object now has the additional hobby:
{ name: "Jane Doe", age: 28, hobbies: ["painting", "hiking", "traveling"]}You can then convert the modified JavaScript object back into YAML using the stringify function:
const updatedYamlData = stringify(jsObject);console.log(updatedYamlData);The updated YAML data would now include the new hobby:
name: Jane Doeage: 28hobbies: - painting - hiking - travelingConclusion
Section titled “Conclusion”Deno’s YAML module makes it easy to work with YAML data in your projects. By using the parse and stringify functions, you can quickly convert between YAML and JavaScript objects, allowing you to manipulate and store data as needed. This powerful and user-friendly module is a great addition to any Deno application that requires YAML support.
© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js