swaroop sg
swaroop sg

Reputation: 73

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 node.js following code, am not getting &color-white. That may be because its omitting special characters while converting. But I need them in my json.

node.js 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;
  }
}; 

Upvotes: 3

Views: 4801

Answers (2)

Anthon
Anthon

Reputation: 76632

You YAML in your file is a document that contains anchors.

Such anchors are normally used to be able to dump direct or indirect self-referential structures. Your example however does not contain aliases that refer to these anchors.

The important part of the YAML specification is within the section linked to above:

Otherwise, anchor names are a serialization detail and are discarded once composing is completed

So anchor names should not be relied upon to be available after loading, and not used to relay information. It is the same with comments in YAML documents.

IMO you don't think you need the string color-white that is part of the anchor &color-white. You should compose that string from the key color by walking over the keys of the mapping that is the value associated with color. That will be white and black, and you can easily create the strings color-white and color-black from those key combinations (and find the associated color values).

That way you don't have to change the YAML document, which you might not have complete control over, and you don't have to do special parsing of a string "&color-neutral-20 \"#222222\"" as @greuze suggests you should do.

Upvotes: 0

greuze
greuze

Reputation: 4398

The YAML modules are working properly, the problem is with your YAML. It is a valid file, but maybe it is not what you want.

Let me quote a part of the YAML spec:

Repeated nodes (objects) are first identified by an anchor (marked with the ampersand - “&”), and are then aliased (referenced with an asterisk - “*”) thereafter.

In your example, color-white is an alias for that node, that could be used in the rest of the yaml file.

If you need the texts after the ampersand (&) symbol to be in the JSON file, you could use a yaml file like:

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

That will return a JSON like:

{
    "color": {
        "white": "&color-white \"#FFFFFF\"",
        "black": "&color-black \"#262626\""
    },
    "neutral": {
        "20": "&color-neutral-20 \"#222222\"",
        "90": "&color-neutral-90 \"#EEEEEE\""
    }
}

So json.color.white will be &color-white "#FFFFFF"

Upvotes: 2

Related Questions