Deno.js Tutorial to Convert JSON File to YAML String Using json2yaml Module Using TypeScript Full Project For Beginners

 

json2yaml

 

A Deno module that converts a JSON string to a (pretty) YAML string 🦕

 

 

Module

 

 

 

import { json2yaml } from "https://deno.land/x/json2yaml/mod.ts";

const jsonString = '{"hello": "world"}';
const yamlString = json2yaml(jsonString);

console.log(yamlString);

 

 

Output:

 

 

hello: world

 

 

You can also specify the number of spaces to use for indents:

 

const jsonString = '{"foo": ["bar", "baz"]}';
const yamlString = json2yaml(jsonString, 3);

console.log(yamlString);

 

 

Output:

foo:
   -  bar
   -  baz

For more documentation, run:

$ deno doc mod.ts

 

 

CLI

# Prints output to stdout
$ deno run --allow-read https://deno.land/x/json2yaml/cli.ts -- input.json

# Write contents into output.yaml
$ deno run --allow-read https://deno.land/x/json2yaml/cli.ts -- input.json > output.yaml

You can also use deno install to easily create an alias command:

$ deno install -f --allow-read -n json2yaml https://deno.land/x/json2yaml/cli.ts

# Much shorter!
$ json2yaml -- input.json

Explanation:

  • -f: Forcefully overrides previous installations of json2yaml
  • --allow-read: Grants read access to the cli
  • -n json2yaml: Sets the alias to json2yaml

Leave a Reply