Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert yaml to json using javascript [duplicate]

I got this yaml file:

description:
  is_a: AnnotationProperty
  labelEN: description
  labelPT: descrição

relevance:
  is_a: AnnotationProperty
  domain: Indicator
  labelEN: relevance
  labelPT: relevância

title:
  is_a: AnnotationProperty
  labelPT: título
  labelEN: title
  range: Literal

and I need to convert it to json, so I can get something like this:

{
    "description": {
        "is_a": "AnnotationProperty",
        "labelEN": "description",
        "labelPT": "descrição"
    },
    "relevance": {
        "is_a": "AnnotationProperty",
        "domain": "Indicator",
        "labelEN": "relevance",
        "labelPT": "relevância"
    },
    "title": {
        "is_a": "AnnotationProperty",
        "labelPT": "título",
        "labelEN": "title",
        "range": "Literal"
    }
}

and save it in a js variable...

So, how can I do this?

Hey, please check link below for YAML to JSON converter https://www.yamlonline.com/

like image 787
Pedro Henrique Calixto Avatar asked May 02 '16 01:05

Pedro Henrique Calixto


1 Answers

You can solve that with a simple javascript script running on node.

  1. install node.js
  2. install the js-yaml package: npm install js-yaml -g

Then save this script into a file, and run it with node.js:

var inputfile = 'input.yml',
    outputfile = 'output.json',
    yaml = require('js-yaml'),
    fs = require('fs'),
    obj = yaml.load(fs.readFileSync(inputfile, {encoding: 'utf-8'}));
// this code if you want to save
fs.writeFileSync(outputfile, JSON.stringify(obj, null, 2));
like image 141
Code Rage Avatar answered Sep 20 '22 14:09

Code Rage