Wednesday, 5 April 2017

converting yaml file to json in nodejs

i have a edit.yml file in my project here is the yaml file

color :
  white : &color-white "#FFFFFF"
  black : &color-black "#262626"
neutral :
  20 : &color-neutral-20 "#222222"
  90 : &color-neutral-90 "#EEEEEE"

so when I convert this to json using nodejs following code, am not getting &color-white. That may be because its omitting special characters while converting. But I need them in my json. please help me.

nodejs code:

var fs = require('fs');
const yaml1 = require('js-yaml');
const YAML = require('yamljs');
module.exports={
save : function(item) {

fs.writeFile('edit1.yml',YAML.stringify(item,4),function(err,item){
  if(err){

  }
});

},
load : function() {
var data = yaml1.safeLoad(fs.readFileSync('edit0.yml','utf8'));
var indentedJson = JSON.stringify(data, null, 4);
return indentedJson;
}

};

please help me.

Thank you



via swaroop sg

No comments:

Post a Comment