Parse options and schemas in yaml
The same YAML text can mean different things depending on the schema and YAML version, and parse options let you choose which rules apply.
How it works
- Integers as BigInt. With
{ intAsBigInt: true }the number 999 comes back as abigintinstead of anumber. [1] - Failsafe schema.
{ schema: 'failsafe' }keeps scalars as strings, so999is returned as the string'999'. [1] - YAML 1.1 booleans and the json schema. By default
Nois a string. Withversion: '1.1'orschema: 'yaml-1.1'it becomesfalse, and thejsonschema rejects it, so the code catches the error. [1]
Run and edit it
It runs in your browser, in a sandboxed frame that cannot see this site; the packages load from esm.sh only after you press Run.
import { parse } from 'yaml'
const show = (label, value) =>
console.log(label.padEnd(36), String(value), '(' + typeof value + ')')
// integers
show("parse('number: 999')", parse('number: 999').number)
show('intAsBigInt: true', parse('number: 999', { intAsBigInt: true }).number)
show("schema: 'failsafe'", parse('number: 999', { schema: 'failsafe' }).number)
// YAML 1.2 core schema vs YAML 1.1
show("parse('No')", parse('No'))
show("version: '1.1'", parse('No', { version: '1.1' }))
show("schema: 'yaml-1.1'", parse('No', { schema: 'yaml-1.1' }))
show("parse('3')", parse('3'))
show("parse('3', failsafe)", parse('3', { schema: 'failsafe' }))
// the json schema only accepts JSON-style scalars
try {
parse('No', { schema: 'json' })
} catch (err) {
console.log("schema: 'json' error:", err.message.split('\n')[0])
}
Try other values such as 'yes', 'on' or '0o14' with and without version: '1.1'. Code written by AgentGid [1].
Try other values such as 'yes', 'on' or '0o14' with and without version: '1.1'. Code written by AgentGid [1].Checked Oct 12, 2026: it starts without errors in Chromium with yaml 2.9.1. Needs WebGL.
More yaml examples
Parse YAML in JavaScript with yaml Convert an object to YAML with yaml Multi-document YAML with yaml Edit YAML without losing comments
Sources
Explanations written by AgentGid from the official docs; the numbers in brackets point here. The code marked "written by AgentGid" is ours and free to copy.