What is json-to-pretty-yaml?
The json-to-pretty-yaml npm package is a utility that allows you to convert JSON objects to YAML format with pretty-printing. This can be useful for configuration files, data serialization, and more.
What are json-to-pretty-yaml's main functionalities?
Convert JSON to YAML
This feature allows you to convert a JSON object to a YAML string. The resulting YAML string is formatted in a human-readable way.
const jsonToPrettyYaml = require('json-to-pretty-yaml');
const jsonObject = { name: 'John', age: 30, city: 'New York' };
const yamlString = jsonToPrettyYaml.stringify(jsonObject);
console.log(yamlString);
Pretty-print YAML
This feature allows you to pretty-print the YAML string with a specified indentation level. This can make the YAML output more readable.
const jsonToPrettyYaml = require('json-to-pretty-yaml');
const jsonObject = { name: 'John', age: 30, city: 'New York' };
const yamlString = jsonToPrettyYaml.stringify(jsonObject, { indent: 4 });
console.log(yamlString);
Other packages similar to json-to-pretty-yaml
js-yaml
js-yaml is a popular package for parsing and dumping YAML. It provides more comprehensive YAML support, including schema validation and custom types. Compared to json-to-pretty-yaml, js-yaml offers more advanced features but may be more complex to use for simple JSON to YAML conversions.
yaml
The yaml package is another library for parsing and stringifying YAML. It supports a wide range of YAML features and is highly configurable. It is similar to js-yaml in terms of functionality but offers a different API. It is more feature-rich compared to json-to-pretty-yaml but also more complex.
♻️ json-to-pretty-yaml
A node module to convert JSON to pretty YAML
Installation
npm install --save json-to-pretty-yaml
Usage
index.js
const fs = require('fs');
const YAML = require('json-to-pretty-yaml');
const json = require('input.json');
const data = YAML.stringify(json);
fs.writeFile('output.yaml', data);
input.json
{
"a": 1,
"b": 2,
"c": [
{
"d": "cool",
"e": "new"
},
{
"f": "free",
"g": "soon"
}
]
}
output.yaml
a: 1
b: 2
c:
- d: "cool"
e: "new"
- f: "free"
g: "soon"
Testing
npm test