Skip to main content

YAML

The ballerina/yaml module (v0.8.0) provides YAML parsing and serialization with support for multiple YAML schemas, custom types, anchor/alias handling, and multi-document streams.

Module​

ballerina/yaml

Usage​

Read YAML from a string​

import ballerina/yaml;

string yamlContent = string `
name: MyService
version: 1.0.0
endpoints:
- path: /api
port: 8080
- path: /health
port: 8081`;

json config = check yaml:readString(yamlContent);

Read YAML from a file​

import ballerina/yaml;

json config = check yaml:readFile("resources/config.yaml");

Write YAML to a string​

import ballerina/yaml;

json data = {
database: {
host: "localhost",
port: 5432,
name: "mydb"
}
};

string[] yamlLines = check yaml:writeString(data);

Write YAML to a file​

import ballerina/yaml;

json config = {
server: {host: "0.0.0.0", port: 8080},
logging: {level: "INFO"}
};

check yaml:writeFile("output.yaml", config);

Multi-document YAML streams​

import ballerina/yaml;

// Read a YAML stream with multiple documents
string multiDoc = string `
---
name: doc1
---
name: doc2`;

json docs = check yaml:readString(multiDoc, isStream = true);

// Write multiple documents
json[] documents = [{name: "doc1"}, {name: "doc2"}];
check yaml:writeFile("multi.yaml", documents, isStream = true);

Custom write configuration​

import ballerina/yaml;

json data = {name: "service", version: "2.0"};

string[] yamlLines = check yaml:writeString(data,
indentationPolicy = 4,
useSingleQuotes = true,
forceQuotes = true
);

Functions​

FunctionSignatureDescription
readStringreadString(string yamlString, *ReadConfig config) returns json|ErrorParse a YAML string into JSON.
readFilereadFile(string filePath, *ReadConfig config) returns json|ErrorParse a YAML file into JSON.
writeStringwriteString(json yamlStructure, *WriteConfig config) returns string[]|ErrorSerialize JSON to YAML string lines.
writeFilewriteFile(string filePath, json yamlStructure, *WriteConfig config) returns Error?Write JSON as YAML to a file.

ReadConfig​

FieldTypeDefaultDescription
schemaYAMLSchemaCORE_SCHEMAYAML schema used for parsing.
isStreambooleanfalseWhether to read a stream of YAML documents.
allowAnchorRedefinitionbooleantrueWhether anchors can be redefined.
allowMapEntryRedefinitionbooleanfalseWhether duplicate map keys are allowed.

WriteConfig​

FieldTypeDefaultDescription
indentationPolicyint2Number of spaces per indentation level.
blockLevelint1Maximum depth for block collection style.
canonicalbooleanfalseWhether to write tags alongside nodes.
useSingleQuotesbooleanfalseWhether to use single quotes for scalars.
forceQuotesbooleanfalseWhether to quote all scalars.
schemaYAMLSchemaCORE_SCHEMAYAML schema used for writing.
isStreambooleanfalseWhether to write a stream of YAML documents.

YAML schemas​

SchemaDescription
FAILSAFE_SCHEMAGeneric schema that works for any YAML document.
JSON_SCHEMASupports all basic JSON types.
CORE_SCHEMAExtension of JSON schema with more human-readable presentation.