Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
Edit yaml files using js function

I am trying to edit a template-yaml file using a js function. The js function will get a name of organization: org and this will replace corresponding values in the template-yaml and generate a org-yaml.

My template-yaml file looks like this:

Organizations:
    - &Org3   # this line here is important.
        Name: Org3Name
        ID: Org3ID

I am using the below function:

const yaml = require('js-yaml');
const fs = require('fs');

const changeConfig = (org) => {
    // doc.Organizations[0] = org;
    doc.Organizations[0].Name = org + 'Name';
    doc.Organizations[0].ID = org + 'ID';
    fs.writeFileSync('new_file.yaml', yaml.dump(doc));
}

let doc = yaml.load(fs.readFileSync('./sample_yaml.yaml', 'utf-8'));
changeConfig('Org1');

I am getting is this:

Organizations:
  - Name: Org1MSP
    ID: Org1MSP

Is there some way I can modify to get a structure like this:

Organizations:
  - Org1  # notice this
    Name: Org1MSP
    ID: Org1MSP
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Kalinda, you can try this:

const yaml = require('js-yaml');
const fs = require('fs');

const changeConfig = (org) => {
    doc.Organizations[0][org] = {};
    delete doc.Organizations[0].Org;
    doc.Organizations[0][org].Name = org + 'Name';
    doc.Organizations[0][org].ID = org + 'ID';
    fs.writeFileSync('new_file.yaml', yaml.dump(doc));
}

let doc = yaml.load(fs.readFileSync('./sample_yaml.yaml', 'utf-8'));
changeConfig('Org2');

Sample yaml file:

Organizations:
    - Org:
        Name: OrgName
        ID: OrgID

if is correct, you need rename or create a new key in yaml file.

Result:

Organizations:
  - Org2:
      Name: Org2Name
      ID: Org2ID

When you use load methods, this methods transform your yaml file in plain object. So you can have better control over your object within javascript.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!