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

384
Views
How to call the JSON value using nodejs when the value is having hyphen

My JSON value has a value with hyphen like database-name.

I wanted to replace the value of the database-name using below code

    result.databases.database-name = 'ArangoDB';

But it is showing error, How to call the JSON value when the value is having hyphen

Here is a code

const fs = require("fs");
const xml2js = require('xml2js');

fs.readFile("databases.xml", "utf-8", (err, data) => {
    if (err) {
        throw err;
    }

    xml2js.parseString(data, (err, result) => {
        if (err) {
            throw err;
        }

        result.databases.database-name = 'ArangoDB';

       
        const builder = new xml2js.Builder();
        const xml = builder.buildObject(result);

        fs.writeFile('new-databases.xml', xml, (err) => {
            if (err) {
                throw err;
            }

            console.log(`Updated XML is written to a new file.`);
        });

    });
});
about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

You can use this syntax to access object members with a string as the key

result.databases["database-name"] = 'ArangoDB';

Here is the reference

about 4 years ago · Santiago Gelvez Report

0

Try to do like this:

result.databases["database-name"]

That's the way to access properties when you have a hyphen in their key names.

about 4 years ago · Santiago Gelvez 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!