Parsing and serializing YAML

YAML is a widely used data serialization language designed to be easily human readable and writeable.

Edit on GitHub

import { parse, stringify } from "jsr:@ende/yaml";

To parse a YAML string, you can use the standard library’s YAML parse function. The value is returned as a JavaScript object.

const text = `
foo: bar
baz:
  - qux
  - quux
`;
const data = parse(text) as { baz: string[]; foo: string; };
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.

const obj = {
  hello: "world",
  numbers: [1, 2, 3]
};

const yaml = stringify(obj);
console.log(yaml);
// hello: world
// numbers:
//   - 1
//   - 2
//   - 3

Run this example locally using the Ende CLI:

ende run https://docs.ende.sh/examples/scripts/parsing_serializing_yaml.ts

Additional resources