This is an implementation of YAML, a human-friendly data serialization language. Started as PyYAML port, it was completely rewritten from scratch. Now it's very fast, and supports 1.2 spec. Show InstallationYAML module for node.js
CLI executableIf you want to inspect your YAML files from CLI, install js-yaml globally:
Usage
APIHere we cover the most 'useful' methods. If you need advanced details (creating your own tags), see examples for more info. const yaml = require('js-yaml'); const fs = require('fs'); // Get document, or throw exception on error try { const doc = yaml.load(fs.readFileSync('/home/ixti/example.yml', 'utf8')); console.log(doc); } catch (e) { console.log(e); } load (string [ , options ])Parses 1 as single YAML document. Returns either a plain object, a string, a number, 2 or 3, or throws 4 on error. By default, does not support regexps, functions and undefined.options:
NOTE: This function does not understand multi-document sources, it throws exception on those. NOTE: JS-YAML does not support schema-specific tag resolution restrictions. So, the JSON schema is not as strictly defined in the YAML specification. It allows numbers in any notation, use 6 and 7 as 2, etc. The core schema also has no such restrictions. It allows binary notation for integers.loadAll (string [, iterator] [, options ])Same as 9, but understands multi-document sources. Applies const yaml = require('js-yaml'); const fs = require('fs'); // Get document, or throw exception on error try { const doc = yaml.load(fs.readFileSync('/home/ixti/example.yml', 'utf8')); console.log(doc); } catch (e) { console.log(e); }0 to each document if specified, or returns array of documents. const yaml = require('js-yaml'); yaml.loadAll(data, function (doc) { console.log(doc); }); dump (object [ , options ])Serializes const yaml = require('js-yaml'); const fs = require('fs'); // Get document, or throw exception on error try { const doc = yaml.load(fs.readFileSync('/home/ixti/example.yml', 'utf8')); console.log(doc); } catch (e) { console.log(e); }1 as a YAML document. Uses 9, so it will throw an exception if you try to dump regexps or functions. However, you can disable exceptions by setting the const yaml = require('js-yaml'); const fs = require('fs'); // Get document, or throw exception on error try { const doc = yaml.load(fs.readFileSync('/home/ixti/example.yml', 'utf8')); console.log(doc); } catch (e) { console.log(e); }3 option to const yaml = require('js-yaml'); const fs = require('fs'); // Get document, or throw exception on error try { const doc = yaml.load(fs.readFileSync('/home/ixti/example.yml', 'utf8')); console.log(doc); } catch (e) { console.log(e); }4. options:
The following table show availlable styles (e.g. "canonical", "binary"...) available for each tag (.e.g. !!null, !!int ...). Yaml output is shown on the right side after 2 (default setting) or 3:
Example: dump(object, { 'styles': { '!!null': 'canonical' // dump null as ~ }, 'sortKeys': true // sort object keys }); Supported YAML typesThe list of standard YAML tags and corresponding JavaScript types. See also YAML tag discussion and YAML types repository.
JavaScript-specific tags See js-yaml-js-types for extra types. CaveatsNote, that you use arrays or objects as key in JS-YAML. JS does not allow objects or arrays as keys, and stringifies (by calling 4 method) them at the moment of adding them.--- ? [ foo, bar ] : - baz ? { foo: bar } : - baz - baz { "foo,bar": ["baz"], "[object Object]": ["baz", "baz"] } Also, reading of properties on implicit block mapping keys is not supported yet. So, the following YAML document cannot be loaded. 0js-yaml for enterpriseAvailable as part of the Tidelift Subscription The maintainers of js-yaml and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more. |