David's Blog

Parsing and Manipulating YAML with Deno's YAML Module

By David Li on 2023-07-24T22:28:00.000Z

Parsing and Manipulating YAML with Deno’s YAML Module

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

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

To parse a YAML string into a JavaScript object, use the parse function:

const yamlData = `
name: John Doe
age: 30
hobbies:
  - 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

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 Doe
age: 28
hobbies:
  - painting
  - hiking

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 Doe
age: 28
hobbies:
  - painting
  - hiking
  - traveling

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.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-11-28.