Reputation: 135
The title is kind of bad, but I will try my best to explain it.
What I'm trying to do is pretty much navigate through a yaml file as you normally would
var doc = yaml.safeLoad(fs.readFileSync('./settings.yml', 'utf8'));
console.log(doc.admin.permissions);
except I want to be able to do this:
var adminrolename = "admin1";
console.log(doc.adminrolename.permissions);
Is there a way I could do this? I've been looking around and I can't seem to find an answer, I might just be searching for the wrong thing, so sorry if this has an obvious answer
Upvotes: 0
Views: 1027
Reputation: 1534
This is what I did for a proof of concept. Remember that keys are case sensitive so doc["admin"]
won't be same as doc["Admin"]
Yaml File:
doc:
admin:
permissions:
- abc
- xyz
admin1:
permissions:
- abc1
- xyz1
JS File:
const yaml = require('js-yaml');
const fs = require('fs');
try {
const config = yaml.safeLoad(fs.readFileSync('test.yml', 'utf8'));
const doc = config.doc;
console.log(doc["admin"].permissions);
console.log(doc["admin1"].permissions);
} catch (e) {
console.log(e);
}
Result:
[ 'abc', 'xyz' ]
[ 'abc1', 'xyz1' ]
Upvotes: 1
Reputation: 135
Sorry everyone for the inconvenience, @Kinaan Khan Sherwani was right, I did try it, but I messed up the yaml file.
doc[adminrolename].permissions
That is what the correct code is
I didn't put any quotes around the sections... facepalms. Here's what the original YAML was:
This is what I changed the YAML to, to fix my problem:
Upvotes: 0