File: dumper.md | Updated: 11/15/2025
This example demonstrates how to dump JavaScript objects to YAML with custom formatting styles.
JS-YAML's dump() function allows you to customize the output format with options like:
{
"name" : "Wizzard",
"level" : 17,
"sanity" : null,
"inventory" : [
{
"name" : "Hat",
"features" : [ "magic", "pointed" ],
"traits" : {}
},
{
"name" : "Staff",
"features" : [],
"traits" : { "damage" : 10 }
},
{
"name" : "Cloak",
"features" : [ "old" ],
"traits" : { "defence" : 0, "comfort" : 3 }
}
]
}
'use strict';
var yaml = require('../');
var object = require('./dumper.json');
console.log(yaml.dump(object, {
flowLevel: 3,
styles: {
'!!int' : 'hexadecimal',
'!!null' : 'camelcase'
}
}));
The flowLevel option determines at which nesting level to switch from block style to flow style:
{} for objects and [] for arraysThe styles option lets you specify how to represent specific YAML types:
0x11 instead of 17)Null instead of nullname: Wizzard
level: 0x11
sanity: Null
inventory:
- name: Hat
features: [magic, pointed]
traits: {}
- name: Staff
features: []
traits: {damage: 0xA}
- name: Cloak
features: [old]
traits: {defence: 0x0, comfort: 0x3}
!!int)binary - Binary format (e.g., 0b1010)octal - Octal format (e.g., 0o12)decimal - Decimal format (default)hexadecimal - Hexadecimal format (e.g., 0xA)!!null)lowercase - nulluppercase - NULLcamelcase - Null~ - Tilde representation!!str)plain - Unquotedsingle - Single quotesdouble - Double quotesliteral - Literal block scalar (|)folded - Folded block scalar (>)This is useful when you need to: