Convert a JSON string to YAML format.
Code
Utilitiesconst toYaml = (obj, indent = 0) => {
const pad = ' '.repeat(indent);
if (Array.isArray(obj)) {
return obj.map(v => `${pad}- ${typeof v === 'object' ? '\n' + toYaml(v, indent + 1) : v}`).join('\n');
}
if (typeof obj === 'object' && obj !== null) {
return Object.entries(obj).map(([k, v]) => {
if (typeof v === 'object') return `${pad}${k}:\n${toYaml(v, indent + 1)}`;
return `${pad}${k}: ${v}`;
}).join('\n');
}
return `${pad}${obj}`;
};
return toYaml(JSON.parse(json));Parameters
JSON string to convert.
Browser·fetch() may be limited by CORS
More JavaScript Snippets
Array to Object
Convert an array of key-value pairs to an object using Object.fromEntries.
Camel Case to Words
Convert a camelCase string to separate words by inserting spaces before capitals.
Convert Currency
Convert between currencies using the Frankfurter API for live exchange rates.
CSV to JSON
Convert a CSV string to a JSON array of objects with headers as keys.
Decimal to Octal
Convert a decimal number to its octal string representation.
Integer to Roman
Convert integer to Roman numeral.