What is yamljs?
The yamljs npm package is a library designed to parse YAML ('YAML Ain't Markup Language') strings and generate JavaScript objects, and vice versa. It provides an easy way to convert data between YAML, a human-friendly data serialization standard, and JavaScript objects. This can be particularly useful for configuration files, data exchange, and more, where YAML's readability and JavaScript's ubiquity are advantageous.
What are yamljs's main functionalities?
Parsing YAML to JavaScript Object
This feature allows you to convert a YAML string into a JavaScript object. It's particularly useful for reading configuration files or data represented in YAML format and using it within a JavaScript application.
const YAML = require('yamljs');
const yamlString = 'greeting: hello\nname: world';
const object = YAML.parse(yamlString);
console.log(object); // Output: { greeting: 'hello', name: 'world' }
Stringifying JavaScript Object to YAML
This feature enables the conversion of JavaScript objects into YAML strings. It's useful for creating configuration files or data exchange where YAML's human-readable format is preferred.
const YAML = require('yamljs');
const object = { greeting: 'hello', name: 'world' };
const yamlString = YAML.stringify(object, 4);
console.log(yamlString); // Output: 'greeting: hello\nname: world'
Other packages similar to yamljs
js-yaml
js-yaml is another popular YAML parsing and serialization library that offers similar functionalities to yamljs. It provides comprehensive YAML support, including the ability to customize schemas, which makes it more flexible in handling various YAML specifications compared to yamljs. js-yaml is known for its performance and extensive feature set.
yaml
The 'yaml' package is a modern YAML parser and stringifier, supporting YAML 1.2. It focuses on being highly compliant with the YAML specification and offers a simple API for parsing and stringifying. Compared to yamljs, it might offer better support for newer YAML features and specifications.
yaml.js
Standalone JavaScript YAML 1.2 Parser & Encoder. Works under node.js and all major browsers. Also brings command line YAML/JSON conversion tools.
Mainly inspired from Symfony Yaml Component.
How to use
Import yaml.js in your html page:
<script type="text/javascript" src="yaml.js"></script>
Parse yaml string:
nativeObject = YAML.parse(yamlString);
Dump native object into yaml string:
yamlString = YAML.stringify(nativeObject[, inline [, spaces ] ]);
Load yaml file:
nativeObject = YAML.load('file.yml');
Load yaml file:
YAML.load('file.yml', function(result)
{
nativeObject = result;
});
Use with node.js
Install module:
npm install yamljs
Use it:
YAML = require('yamljs');
nativeObject = YAML.parse(yamlString);
yamlString = YAML.stringify(nativeObject, 4);
nativeObject = require('./myfile.yml');
nativeObject = YAML.load('myfile.yml');
Command line tools
You can enable the command line tools by installing yamljs as a global module:
npm install -g yamljs
Then, two cli commands should become available: yaml2json and json2yaml. They let you convert YAML to JSON and JSON to YAML very easily.
yaml2json
usage: yaml2json [-h] [-v] [-p] [-i INDENTATION] [-s] [-r] [-w] input
Positional arguments:
input YAML file or directory containing YAML files.
Optional arguments:
-h, --help Show this help message and exit.
-v, --version Show program's version number and exit.
-p, --pretty Output pretty (indented) JSON.
-i INDENTATION, --indentation INDENTATION
Number of space characters used to indent code (use
with --pretty, default: 2).
-s, --save Save output inside JSON file(s) with the same name.
-r, --recursive If the input is a directory, also find YAML files in
sub-directories recursively.
-w, --watch Watch for changes.
json2yaml
usage: json2yaml [-h] [-v] [-d DEPTH] [-i INDENTATION] [-s] [-r] [-w] input
Positional arguments:
input JSON file or directory containing JSON files.
Optional arguments:
-h, --help Show this help message and exit.
-v, --version Show program's version number and exit.
-d DEPTH, --depth DEPTH
Set minimum level of depth before generating inline
YAML (default: 2).
-i INDENTATION, --indentation INDENTATION
Number of space characters used to indent code
(default: 2).
-s, --save Save output inside YML file(s) with the same name.
-r, --recursive If the input is a directory, also find JSON files in
sub-directories recursively.
-w, --watch Watch for changes.
examples
yaml2json myfile.yml
yaml2json myfile.yml > ouput.json
yaml2json myfile.yml --pretty
yaml2json myfile.yml --pretty --save
yaml2json mydirectory --pretty --save --recursive
json2yaml myfile.json > ouput.yml
json2yaml myfile.json --depth 8
json2yaml myfile.json --indentation 4
json2yaml mydirectory --pretty --save --recursive
Important
Symfony dropped support for YAML 1.1 spec. This means that yes
, no
and similar no longer convert to their boolean equivalents.
The internal Yaml().load()
and Yaml().loadFile()
methods renamed to Yaml().parse()
and Yaml().parseFile()
respectively. Exceptions replaced with YamlParseException
object.