There are two ways to convert YAML to JSON
Using Node.Js:
First, deploy the js-yaml
library using npm:
Then, use the subsequent Node.Js code:
1
2
3
4
5
6
7
|
const fs = require('fs');
const yaml = require('js-yaml');
const yamlData = fs.ReadFileSync('enter.yaml', 'utf8');
const jsonData = yaml.SafeLoad(yamlData);
fs.WriteFileSync('output.Json', JSON.Stringify(jsonData, null, 2));
|
Replace ‘input.Yaml’ along with your YAML report and ‘output.Json’ with the preferred JSON output report.
Using Pure JavaScript:
You can use the yaml
library. First, consist of it to your HTML record:
1
|
<script src="https://unpkg.Com/yaml/browser/yaml.Js"></script>
|
Then, use the subsequent JavaScript code:
1
2
3
4
5
6
7
8
9
10
11
|
const yamlData = `
key1: value1
key2:
- item1
- item2
`;
const jsonData = yaml.Parse(yamlData);
const jsonString = JSON.Stringify(jsonData, null, 2);
console.Log(jsonString);
|
Adjust the yamlData
variable with your YAML content material. Note that using a library like yaml
is a good exercise for handling YAML in a browser surroundings.
Choose the method that satisfactory suits your surroundings and requirements.
Sharing is caring!