json2yaml: JSON to YAML Converter

1.1.0 · abandoned · verified Sun Apr 19

This package, `json2yaml`, provides a command-line utility and a programmatic API for converting JSON data into human-readable YAML format. First released with a Node.js `0.2.0` engine requirement, its current stable version is 1.1.0. The tool primarily aims to pretty-print JSON, transforming it into the more visually appealing, whitespace-based YAML notation, leveraging the fact that JSON is technically a proper subset of YAML. This means any valid JSON is also valid YAML, but `json2yaml` focuses on converting to the more common, indented YAML style for improved human readability. The project appears to have ceased active development around 9 years ago, suggesting it is no longer actively maintained and may have compatibility issues with modern JavaScript environments.

Common errors

Warnings

Install

Imports

Quickstart

This code snippet demonstrates how to use `json2yaml` programmatically to convert a JavaScript object (which translates to JSON) into a human-readable YAML string using the `stringify` method.

const json2yaml = require('json2yaml');

// Sample JSON data for conversion
const data = {
  product: 'Widget X',
  version: '1.0.0',
  features: [
    { name: 'Feature A', enabled: true },
    { name: 'Feature B', enabled: false, note: 'Experimental' }
  ],
  settings: {
    debugMode: false,
    logLevel: 'info'
  },
  nullValue: null,
  price: 99.99,
  available: true
};

try {
  // Convert the JSON object to a YAML string
  const yamlOutput = json2yaml.stringify(data);
  console.log('--- Converted YAML Output ---\n');
  console.log(yamlOutput);
} catch (error) {
  console.error('An error occurred during JSON to YAML conversion:', error.message);
}

view raw JSON →