Parse YAML in JavaScript with yaml
parse() is the simplest way to read YAML: it works like JSON.parse, but takes YAML text and returns plain JavaScript values.
How it works
- Import parse.
parseis a named export of the package. The docs describe it as the YAML counterpart ofJSON.parse. [1] - Read typed values. The code parses a small config and prints each field with its JavaScript type. Mappings become objects, sequences become arrays, and scalars become numbers, booleans, strings or
null. [1] - Flow sequences. The last line parses an inline
[ ... ]sequence, the same input the README shows, wheremaybestays a string. [2]
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 src = `
app: demo
port: 8080
debug: false
tags: [api, web]
owner:
name: Ada
team: platform
ratio: 3.14159
nothing: null
`
const data = parse(src)
console.log('app:', data.app)
console.log('port:', data.port, '(' + typeof data.port + ')')
console.log('port + 1:', data.port + 1)
console.log('debug:', data.debug, '(' + typeof data.debug + ')')
console.log('tags:', JSON.stringify(data.tags), Array.isArray(data.tags) ? '(array)' : '')
console.log('owner.name:', data.owner.name)
console.log('owner.team:', data.owner.team)
console.log('ratio:', data.ratio)
console.log('nothing:', data.nothing)
console.log('flow sequence:', JSON.stringify(parse('[ true, false, maybe, null, 3.14159 ]')))
Add a new key to the YAML text and print it, or change port to a quoted string to see its type change. Code written by AgentGid [1].
Add a new key to the YAML text and print it, or change port to a quoted string to see its type change. 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
Convert an object to YAML with yaml Parse options and schemas in 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.