YAML

YAML is way more complex than JSON. In fact, JSON syntax is included in YAML. YAML is quite human-readable πŸš€, so in some cases, it may be better to use YAML rather than JSON.

  • List
# all are the same
list1:
  - a
  - b
  - c
list2: [a, b, c]
list3: ['a', 'b', 'c']
  • Keys
key1: value
key2: {}
key3:
  - true
  - 5
  - "toto"
  • Texts (inline and multiline texts)
inline: This is an inline text
multiline_text1: This is a really really really
  long long text. 
  And this is still part of the text πŸš€.
multiline_text2: >
  Same.
code: |+
  preserve newlines and indents

Read YAML files in Node.js

js-yaml

js-yaml (6.1k ⭐) is frequently updated.

const yaml = require('js-yaml');
const fs   = require('fs');

const xxx = yaml.load(fs.readFileSync('test.yaml', 'utf8'));
console.log(xxx);

In TypeScript, you may use the following import:

// @ts-ignore
import { load } from 'js-yaml';

yamljs

yamljs (0.9k ⭐) was not updated for years.

const YAML = require('yamljs');
const fs   = require('fs');

const xxx = YAML.parse(fs.readFileSync('test.yaml', 'utf8'));
console.log(xxx)

πŸ‘» To-do πŸ‘»

Stuff that I found, but never read/used yet.

tags:
  - xxx: &xxx "XXX"

entries:
  yyy: *xxx