json-to-yaml-converter
v1.0.5
Published
A simple utility to convert JSON Objects or json files to yaml contents
Downloads
4
Maintainers
Readme
JSON to YAML Converter
json-to-yaml-converter A simple yet powerful utility for converting JSON objects / JSON files to YAML format. This package provides an easy way to read JSON content from an object file and convert it into a human-readable YAML format. The converted YAML can also be saved to an output file if specified.
Features:
Convert JSON to YAML: Read JSON content from a file or from json object and convert it to YAML format.
Optional File Output: Save the converted YAML content to a specified output file.
Easy Integration: Simple API for seamless integration into your projects.
Example Usage:
Installation
npm install json-to-yaml-converter
Direct JSON Code Example 1
Example 1
const { jsonToYaml, convertJsonFileToYaml } = require('json-to-yaml-converter');
//single level
const json = {
name: 'MyApp',
version: '1.0.0',
description: 'An example app'
};
const yamlString = jsonToYaml(json);
console.log(yamlString);
Output 1
name: MyApp
version: 1.0.0
description: An example app
Direct JSON Code Example 2
Example 2
const { jsonToYaml, convertJsonFileToYaml } = require('json-to-yaml-converter');
//two level
const json = {
"app": {
"name": "MyApp",
"version": "1.0.0",
"description": "An example app"
}
}
const yamlString = jsonToYaml(json);
console.log(yamlString);
Output 2
app:
name: MyApp
version: 1.0.0
description: An example app
Direct JSON Code Example 3
Example 3
const { jsonToYaml, convertJsonFileToYaml } = require('json-to-yaml-converter');
//three level
const json = {
"app": {
"name": "MyApp",
"version": "1.0.0",
"description": "An example app",
"database": {
"host": "localhost",
"port": 5432,
"name": "myapp_db"
}
}
}
const yamlString = jsonToYaml(json);
console.log(yamlString);
Output 3
app:
name: MyApp
version: 1.0.0
description: An example app
database:
host: localhost
port: 5432
name: myapp_db
Code Example (read from json file)
input JSON file name is 'example.json'
output YAML file name is 'output.yaml'
const { jsonToYaml, convertJsonFileToYaml } = require('json-to-yaml-converter');
// Convert JSON to YAML and save to output.yaml
const yamlString = convertJsonFileToYaml('./example.json', './output.yaml');
if (yamlString) {
console.log('YAML content:');
console.log(yamlString);
}
Example 2
input JSON file name is 'sample1.json'
output YAML file name is 'output1.yaml'
const { jsonToYaml, convertJsonFileToYaml } = require('json-to-yaml-converter');
const yamlString = convertJsonFileToYaml('./sample1.json', 'output1.yaml');
if (yamlString) {
console.log(yamlString);
}
Example 3
input JSON file name is 'sample2.json'
output YAML file name is 'output2.yaml'
const {jsonToYaml, convertJsonFileToYaml } = require('json-to-yaml-converter');
const yamlString = convertJsonFileToYaml('./sample2.json', 'output2.yaml');
if (yamlString) {
console.log(yamlString);
}
Proprietary License
All rights reserved.
This software and its source code are the property of myself i.e. Sampurna Atmaramani. Unauthorized copying, modification, distribution, or use of this software, via any medium, is strictly prohibited.
For inquiries regarding the use of this software, please contact [email protected]