JavaScript:
Working with YAML
How to:
In JavaScript, working with YAML typically involves using a third-party library since the language doesn’t include a built-in parser for YAML. One of the most popular libraries for this purpose is js-yaml
. You can use js-yaml
to parse YAML into JavaScript objects and vice versa.
First, you need to install js-yaml
:
npm install js-yaml
Then, you can use it in your projects. Here’s how you can load a YAML file and parse it into a JavaScript object:
// Require the js-yaml module
const yaml = require('js-yaml');
const fs = require('fs');
// Load YAML from a file
try {
const doc = yaml.load(fs.readFileSync('./config.yaml', 'utf8'));
console.log(doc);
} catch (e) {
console.error(e);
}
If your config.yaml
file looks like this:
version: 1
services:
web:
image: "myapp/web:latest"
ports:
- "5000:5000"
The output will be:
{ version: 1,
services:
{ web:
{ image: 'myapp/web:latest',
ports: [ '5000:5000' ] } } }
To do the reverse, converting a JavaScript object to a YAML string:
const yaml = require('js-yaml');
const obj = {
version: 1,
services: {
web: {
image: "myapp/web:latest",
ports: ["5000:5000"]
}
}
};
const yamlStr = yaml.dump(obj);
console.log(yamlStr);
This code will produce:
version: 1
services:
web:
image: myapp/web:latest
ports:
- '5000:5000'
Using js-yaml
, you can easily integrate YAML parsing and serialization into your JavaScript projects, enhancing data interexchangeability and configuration management.