Burdell
Burdell

Reputation: 60

How to print full yaml or json object to file with node

I am trying to copy the definitions portion of a yaml file into a js doc for a codegen project. I tried using regular expressions (which worked great for copying methods out of swagger generated js files) but apparently regexp does not handle info from yaml files very well. I managed to print MOST of what I want to the commandline through console.log. There are a few arrays that just say [Object], which is problematic. I would like to have their full contents printed. HOWEVER, this is not the main problem. When I try to write this output to a file instead of the console...it just says "[object Object] [object Object]" for my 2 definitions. Has anyone done anything like this before? Here is a snippet of my code and what the console output looks like vs the two lines above TIA!

var doc = yaml.safeLoad(fs.readFileSync('path to my file\swagger.yaml', 'utf8'));
for(var d in doc['definitions']){
    logit(doc['definitions'][d]); //logit write to consle and a file
}

enter image description here

Upvotes: 0

Views: 2712

Answers (1)

Helen
Helen

Reputation: 97932

safeLoad suggests you are using the js-yaml library. It also provides the safeDump and dump methods.

yamlDef= yaml.safeDump(doc['definitions'][d]);
logit(yamlDef);

to convert YAML to JSON:

var json = JSON.stringify(yamlDef);

Upvotes: 1

Related Questions