@catalystic/json-to-yaml
v1.0.0
Published
catalystic package for converting json to yaml
Downloads
825
Readme
json-to-yaml
@catalystic/json-to-yaml
is a javascript npm package to convert JSON data in YAML.
It exports an convert
function which intake JSON data and output YAML data.
You can pass standard JSON or any Javascript object (with primitives, null only and same apply to any nested array or object), check examples below.
Usage
Using Javascript Object
import { convert } from "@catalystic/json-to-yaml";
const json = {
Type: "AWS::AppSync::Resolver",
DependsOn: "GraphQLSchema",
Properties: {
ApiId: { "Fn::GetAtt": "GraphQLApi.ApiId" },
DataSourceName: { "Fn::GetAtt": "UserServiceLambdaDataSource.Name" },
TypeName: "Mutation",
FieldName: "createUser",
Kind: "UNIT",
Runtime: {
Name: "APPSYNC_JS",
RuntimeVersion: "1.0.0",
},
},
};
const yaml = convert(json);
/* --- yaml ---
Type: "AWS::AppSync::Resolver"
DependsOn: "GraphQLSchema"
Properties:
ApiId:
Fn::GetAtt: "GraphQLApi.ApiId"
DataSourceName:
Fn::GetAtt: "UserServiceLambdaDataSource.Name"
TypeName: "Mutation"
FieldName: "createUser"
Kind: "UNIT"
Runtime:
Name: "APPSYNC_JS"
RuntimeVersion: "1.0.0"
*/
Using JSON data
import { readFileSync } from "node:fs";
import { convert } from "@catalystic/json-to-yaml";
const json = readFileSync("./file-name.json");
const yaml = convert(json);
console.log(yaml);
Motivation
On searching the npm registry for some package to convert json to yaml, i stumbled upon json-to-pretty-yaml which was last updated on 27th april 2018. And two of its dependencies were remedial and remove-trailing-whitespaces which were also last updated similarly. The remove-trailing-whitespaces is a one-liner package and remedial is a very small package and they could use modern javascript functions and can be merged into one single ESM package and thus begin the effort to do so and give birth to @catalystic/json-to-yaml.