Skip to content

Convert JSON to YAML

Updated: at 05:22 PM

Using NPM package

We can use https://github.com/eemeli/yaml NPM package.

File convert.js

const YAML = require("yaml");

const jsonObject = {
  version: "1.0.0",
  dependencies: {
    yaml: "^1.10.0",
  },
  package: {
    exclude: [".idea/**", ".gitignore"],
  },
};

const doc = new YAML.Document();
doc.contents = jsonObject;

console.log(doc.toString());

Output

You can run above file as node convert.js. This will give you the following output

version: 1.0.0
dependencies:
  yaml: ^1.10.0
package:
  exclude:
    - .idea/**
    - .gitignore
If you enjoy the content, please consider supporting work