Beginner
clideployweb
Parsing and serializing YAML
YAML is a widely used data serialization language designed to be easily human readable and writeable.
!--frsh-copybutton:1-->
import { parse, stringify } from "https://deno.land/std@0.175.0/encoding/yaml.ts";
To parse a YAML string, you can use the the standard library's YAML parse function. The value is returned as a JavaScript object.
!--frsh-copybutton:2-->
const text = `
foo: bar
baz:
- qux
- quux
`;
const data = parse(text);
console.log(data.foo);
console.log(data.baz.length);
To turn a JavaScript object into a YAML string, you can use the standard library's YAML stringify function.
!--frsh-copybutton:3-->
const obj = {
hello: "world",
numbers: [1, 2, 3],
};
const yaml = stringify(obj);
console.log(yaml);
// hello: word
// numbers:
// - 1
// - 2
// - 3
Run this example locally using the Deno CLI:
deno run https://byexample-wwv03xf36j0g.deno.dev/parsing-serializing-yaml.ts
Additional resources: